Your first test with Python vs WebDriver vs PyCharm

Autor: | 18 listopada 2014|Blog, Techniczne|9 Comments|Wyświetlenia: 29298

Today I’d like You to try Python for Your functional Web tests. Why Python? Because I believe it’s much easier to start with if we’ll compare with Java or .Net and also in several cases Python (because it’s scripting language) can do much more things than even Java (with all cross platform functionality itself).

So let’s do everything from the scratch. Next screens and instructions are for Windows users because I’m sure that install and run Python on OSX or Linux is much easier and doesn’t need any specific explanations.

1. Install Python.

For this we’re going to https://www.python.org/downloads/ and download needed package.

1

2

As You can see that we’re able to download Windows installer and install Python without any problems – I did it in default directory, also have to underline that I’ll use Python 3.4.2 for this example.

3

After installation we’ll need to create local variable PYTHON_HOME (which is really not mandatory, but will be useful) and check if Python added to the main Path. For that just follow screens below one by one.

4

5

6 7

If everything correct we have to able to see next screen after running command “python  –version” in our command prompt.

8

And this is pretty much all for Python installation.

2. Selenium installation.

Here will be also pretty much straightforward process with adding Python language bindings for WebDriver. For Python we don’t need to download different browser drivers as we did for Java.

First open https://pypi.python.org/pypi/selenium – and download archive with selenium (see screen below). After downloading extract somewhere on the disk. Next go to Your Python folder e.g. “C:\Python34” and run command (in command line prompt) – “pip install –U selenium”.

1 2

After this command Python should install Selenium but I faced one issue during this installation and for that we need our extracted Selenium archive – You have to copy extracted folder “selenium” (with all files in it) to “C:\Python34\Lib” (see screen below).

3

One more step – we have to ensure that “selenium” is also in the path variable (scree below).

4

We’re almost ready, actually we’re ready for writing and running scripts in Python. As I mentioned before Python scripting language so it doesn’t need any specific IDE or anything like that – we can use simple notepad for testing. But for future convenience I’d recommend You install PyCharm from JetBrains.

3. Installing PyCharm

I believe one really easy step – go to https://www.jetbrains.com/pycharm/download/ and download community edition, which totally free and provides all necessary functionality. For those who use IntelliJ Idea already – You just need to add Python language plugin and then create new Python project (also pretty simple)

1

After downloading and installing PyCharm we’re all set, so we have just one more step – actual test writing and running.

4. Writing and running first test.

On first two screens we’re creating new project in PyCharm – pretty straight forward process.

1 2

Then we have to check our settings for ensure our PyCharm will use needed Python interpreter. So go : “File > Settings > Project interpreter” or press “Ctrl + Alt + S” and make sure that correct Python distributive selected also there should be “selenium” in the list of packages (see screen below).

3

Last step – create a “test” package in project root directory and then Python file – e.g. “FirstWebDriverTest.py”.

Our test will do several simple steps:

  • Open browser (for this example I’ll use Chrome, but You can easily change that).
  • Navigate to http://www.allegro.pl.
  • Type in search box “Laptop”.
  • Press “Search” button.
  • Verify that first item from results list has “Laptop” in it’s title.

4

For Your convenience I’ll paste whole code below:


import unittest

from selenium import webdriver

class FirstWebDriverTest(unittest.TestCase):

    def set_up(self):

        self.driver = webdriver.Chrome()

        self.driver.get("http://www.allegro.pl")

        self.SEARCH_TEXT = "Laptop"

        self.SEARCH_FIELD_ID = "//input[@id='main-search-text']"

        self.SEARCH_BUTTON_ID = "//input[@class='search-btn']"

        self.SEARCH_RESULT_ITEM_TITLE = "//article[@class='offer offer-brand']//h2//span"

    def test_search_in_allegro_pl(self):

        driver = self.driver

        driver.find_element_by_xpath(self.SEARCH_FIELD_ID)\

.send_keys(self.SEARCH_TEXT)

        search_button = driver.find_element_by_xpath(self.SEARCH_BUTTON_ID)

        search_button.submit()

        driver.implicitly_wait(5000)

        result = driver.find_elements_by_xpath(self.SEARCH_RESULT_ITEM_TITLE)

.__getitem__(0)

.__getattribute__("text")

        self.assertIn(self.SEARCH_TEXT, result)

    def tear_down(self):

        self.driver.close()

If You’ll do copy and paste from this page make sure that after inserting code has original formatting (You can refer to screen above) because for Python it’s extremely important.

So now we can run our test – “Run > Run” or “Alt + Shift + F10

Also You can do it through command prompt line (see screen below)

9

Hope this will be easy for You to start writing tests in Python and You’ll check how actually powerful this language is. One last thing – for this example with all downloading and installing steps I’ve spent about one hour (actually I’ve spent more for doing screens and describing steps).

Your friend is interested to start writing in Python? Please share this article with him.

Autor: | 18 listopada 2014|Blog, Techniczne|9 Comments|Wyświetlenia: 29298
Hey, I’m Oleksandr and I joined “Kainos” at the end of March 2014. Previously I’ve been working for the biggest Ukrainian software company for several years and now I’m working as a Test Automation Engineer on Workday QA project. If someone will ask me about my work – I’ll say it’s all about passion: passion in finding, checking, fixing or even simply exploring. During my free time I like either reading something interesting or going out to check out how life is going all around.

9 Komentarze

  1. definitely not a python develo 25 Lis 2014 w 4:14 pm- Odpowiedz

    What benefits does Python brings in comparison to any other language?

  2. Oleksandr Perepelytsya 26 Lis 2014 w 9:55 am- Odpowiedz

    Let me point just some of pluses: easy to start, easy to maintain, no need to compile (thus no special requirements for environment) – put all those factors close enough to business needs and You’ll get pretty handy tool.

  3. brd 3 Gru 2014 w 10:04 pm- Odpowiedz

    Hi Oleksandr,

    When i’m trying to run code from your post i have an error:

    Error
    Traceback (most recent call last):
    File „C:workspaceFirstWebDriverTesttestFirstWebDriverTest.py”, line 27, in test_search_in_allegro_pl
    driver = self.driver
    AttributeError: ‚FirstWebDriverTest’ object has no attribute ‚driver’

    I’m guesing there is a problem with set_up method.

    • JungleJim 27 Lut 2016 w 1:46 pm- Odpowiedz

      Same error for me too : (

  4. Oleksandr Perepelytsya 4 Gru 2014 w 11:41 am- Odpowiedz

    Hi, I believe the only problem is because of formatting – so just try to copy paste code, then remove redundant lines (which were added by formatter) and it should work (cause it does for me). If not just let me know – I can send that file to Your email.

    • brd 4 Gru 2014 w 5:55 pm- Odpowiedz

      Hi again, the problem was in the names of set_up and tear_down method.
      When I changed them to setUp and tearDown to override methods in unittest.TestCase test move forward but fail on:

      selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {„method”:”xpath”,”selector”:”//input[@class=’search-btn’]”}

      Probably class was changed to: class=”sprite search-btn”.
      I used instead: value=’Szukaj’ and test passed

      Thanks for showing PyCharm, I think I will move to it from Eclipse.

      • Oleksandr Perepelytsya 5 Gru 2014 w 10:56 am- Odpowiedz

        No problem, anyway it’s kind of strange – cause I simple copy – pasted that from PyCharm. But in any case glad that You’re succeed with that.

  5. LusineHovsepyan 11 Cze 2016 w 5:50 pm- Odpowiedz

    Hi, now i have FF47 and selenium last version 2.53.5 , when i run this file i get this error , can you advise ?

    Thanks,

  6. wiki.fristik.sk 15 Paź 2016 w 7:31 pm- Odpowiedz

    In my opinion it is just about the most important info for me personally.
    I am also happy reading your post. Although need to comment
    on few basic problems, the web site style is ideal, the
    content is really nice : D. Good job, cheers

Zostaw komentarz

First test with Python + WebDriver + PyCharm

Autor: | 10 listopada 2014|Uncategorized|0 komentarzy|Wyświetlenia: 20638

Here will be actual post

Autor: | 10 listopada 2014|Uncategorized|0 komentarzy|Wyświetlenia: 20638
Hey, I’m Oleksandr and I joined “Kainos” at the end of March 2014. Previously I’ve been working for the biggest Ukrainian software company for several years and now I’m working as a Test Automation Engineer on Workday QA project. If someone will ask me about my work – I’ll say it’s all about passion: passion in finding, checking, fixing or even simply exploring. During my free time I like either reading something interesting or going out to check out how life is going all around.

Zostaw komentarz