S0-E2/E30 :)

How do you start your first Selenium Grid Test? Yesterday I've given you instructions how to create your Selenium Grid environment. Today I'll focus on how to create your first test that will use this env. Check it Out!

Selenium Grid.

This article is connected with How-To-Start-With-Selenium-Grid - check it out how to create your Selenium Grid Environment :)

Your First testcase.

What to start with? You can start with your own Selenium Test and change it into Selenium Grid one :)

Let's check this example:

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class PythonOrgSearch(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_search_in_python_org(self):
        driver = self.driver
        driver.get("https://github.com")
        assert "GitHub" in driver.title
        elem = driver.find_element_by_name("q")
        elem.send_keys("anselmos")
        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()

OR download it from here

While I tried to run this on my machine, I've used pip to install selenium, then I've found a problem :

.env/local/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
    WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

To fix this issue you have to :

1. Download a geckodriver from here

As I'm using an x64 bit Linux, I chose geckodriver-v0.19.1-linux64.tar.gz and downloaded it with

wget https://github.com/mozilla/geckodriver/releases/download/v0.19.1/geckodriver-v0.19.1-linux64.tar.gz

Then ungzip it:

tar -zxvf geckodriver-v0.19.1-linux64.tar.gz

And moved into path which is executable: (Warning - you'll need root's permissions to move to /usr path)

mv geckodriver /usr/local/bin/

And then I could finally run the script with :

python your-non-selenium-grid-test.py

Now to make this Test run on our Selenium Grid, just change the "driver" in script with:

self.driver = webdriver.Remote(
    command_executor='http://127.0.0.1:4444/wd/hub',
    desired_capabilities={'browserName': 'firefox', 'javascriptEnabled': True}
)

Final Result - A working example of Selenium Grid Test

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class PythonOrgSearch(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Remote(
            command_executor='http://127.0.0.1:4444/wd/hub',
            desired_capabilities={'browserName': 'firefox', 'javascriptEnabled': True}
        )

    def test_search_in_python_org(self):
        driver = self.driver
        driver.get("https://github.com")
        assert "GitHub" in driver.title
        elem = driver.find_element_by_name("q")
        elem.send_keys("anselmos")
        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main()

You can download it also from here.

Execution on more than one node!

If you need to have more than one selenium-node type (besides Firefox), you can customize the script with the following:

First, create a generalized Unittest that will have only test-cases.

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class GenericSeleniumTest(unittest.TestCase):

    def setUp(self):
        self.driver = False

    def test_search_in_python_org(self):
        if not self.driver:
            return True
        driver = self.driver
        driver.get("https://github.com")
        assert "GitHub" in driver.title
        elem = driver.find_element_by_name("q")
        elem.send_keys("anselmos")
        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source

    def tearDown(self):
        if self.driver:
            self.driver.close()

class FirefoxTest(GenericSeleniumTest):
    def setUp(self):
        self.driver = webdriver.Remote(
            command_executor='http://127.0.0.1:4444/wd/hub',
            desired_capabilities={'browserName': 'firefox', 'javascriptEnabled': True}
        )

class ChromeTests(GenericSeleniumTest):
    def setUp(self):
        self.driver = webdriver.Remote(
            command_executor='http://127.0.0.1:4444/wd/hub',
            desired_capabilities={'browserName': 'chrome', 'javascriptEnabled': True}
        )

if __name__ == "__main__":
    unittest.main()

And that's it! You have a fully working and customizable Tests. You can now extend Generalized tests that will be run on each platform or perform a specific test designed only for Firefox or Chrome.

Acknowledgement

Thanks!

That's it :) Comment, share or don't :)

I hope you enjoyed this episode as much as I did :)

BTW: I have something extra for you - since I've used yesterdays commands, I created a simpler script for non-network compatible docker-engines that creates a selenium grid out of the box Check it Here (Only for security reasons I'm putting Md5Sum of this file: 7531d8a83d2f9344a752b203e8e1198c run_selenium_grid.sh ) :)

If you have any suggestions what I should blog about in the next articles - please give me a hint :)

See you tomorrow! Cheers!



Comments

comments powered by Disqus