[笔记]Selenium Testing Tools Cookbook_Chapter13

Chapter 13 Cross-Browser Testing

13.1 Setting up Selenium Grid Server for parallel execution

Download the latest Selenium Server standalone JAR file from http://docs.seleniumhq.org/download/
Go to the folder and setting up a Hub:

java -jar selenium-server-standalone-3.141.0.jar -port 4444 -role hub

-nodeTimeout report Error

Browse to http://localhost:4444/grid/console

13.2 Adding nodes to Selenium Grid for cross-browser testing

Adding a Firefox node

java -jar selenium-server-standalone-3.141.0.jar -role webdriver -hubHost localhost -hubPort 4444 -browser "browserName=firefox,version=72.0.2,setjavascriptEnabled=true,acceptSslCerts=true,maxInstances=2,Platform=MAC" -port 5555

Adding a Chrome node

java -jar selenium-server-standalone-3.141.0.jar -role webdriver -hubHost localhost -hubPort 4444 -browser "browserName=chrome,version=70,setjavascriptEnabled=true,acceptSslCerts=true,maxInstances=2,Platform=MAC" -port 6666

13.3 Creating and executing the Selenium script in parallel with Python

Create and execute tests in parallel with Python bindings using the subprocess module.
test the application functionality using Firefox browser(test_on_firefox.py):

import unittest
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class OnFirefox(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub',\
            desired_capabilities = DesiredCapabilities.FIREFOX)

    def test_JianShu(self):
        driver = self.driver
        driver.get("https://www.jianshu.com")
        SearchArea = driver.find_element_by_name("q")
        SearchArea.send_keys("abc")
        SearchArea.submit()

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

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

test the application functionality using the Chrome browser(test_on_chrome.py):

import unittest
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class OnFirefox(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub',\
            desired_capabilities = DesiredCapabilities.CHROME)

    def test_JianShu(self):
        driver = self.driver
        driver.get("https://www.jianshu.com")
        SearchArea = driver.find_element_by_name("q")
        SearchArea.send_keys("abc")
        SearchArea.submit()

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

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

Create a Python script, which will use the subprocess module to run these test concurrently on different nodes.

import glob
from subprocess import Popen

tests = glob.glob("test*.py")
processes = [Popen('python3 %s' % test, shell = True) for test in tests]

for process in processes:
    process.wait()

13.4 Using Cloud tools for cross-browser testing running tests in the Cloud

In this section, we will set up and run a test in BrowserStack(browserstack.com)

  1. obtain a username and access keys. Code sample:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_cap = {
 'browser': 'Firefox',
 'browser_version': '72.0',
 'os': 'OS X',
 'os_version': 'Catalina',
 'resolution': '2880x1800',
 'name': 'Bstack-[Python] Sample Test'
}

driver = webdriver.Remote(
    command_executor='https://USERNAME:[email protected]/wd/hub',
    desired_capabilities=desired_cap)

driver.get("http://www.jianshu.com")

Search = driver.find_element_by_name("q")
Search.send_keys("abc")
Search.submit()

driver.quit()
  1. execute the test from your command line
  2. verify the results. The Selenium Webdriver test should have opened a URL, inputted a string, submitted the form and returned the page title.

你可能感兴趣的:([笔记]Selenium Testing Tools Cookbook_Chapter13)