Package pytest_auto :: Module selenium_plugin
[hide private]
[frames] | no frames]

Source Code for Module pytest_auto.selenium_plugin

  1  import pytest 
  2  from selenium import webdriver 
  3  from tlib.base import AndroidHelper 
  4  from tlib.base import TestHelper 
  5  from tlib.base.TestHelper import webdriver_chrome_executable, webdriver_ie_executable 
6 7 -def pytest_addoption(parser):
8 """ 9 Options supported by pytest's selenium plug-in 10 """ 11 group = parser.getgroup("selenium", "Testing with the selenium framework") 12 group.addoption( 13 "--base_url", 14 action="store", 15 dest="base_url", 16 help="base url of the server used for testing") 17 18 group.addoption( 19 "--mobile_base_url", 20 action="store", 21 dest="mobile_base_url", 22 help="mobile base url of the server used for testing") 23 24 group.addoption( 25 "--browser", 26 action="append", 27 dest="browser", 28 choices=["chrome", "firefox", "ie", "android"], 29 help="Browser used for running test cases.") 30 31 group.addoption( 32 "--proxy", 33 action="store", 34 dest="proxy", 35 help="""Use provided IP and port as proxy server 36 Sample usage: 37 py.test --proxy=127.0.0.1:8080""") 38 39 group.addoption( 40 "--serial_id", 41 action="store", 42 dest="serial_id", 43 help="Serial of the android device or simulator where Android tests will be run")
44
45 46 -def pytest_generate_tests(metafunc):
47 """ 48 Parses command line --browser and parametrizes tlib_tests with each browser specified 49 """ 50 browsers = metafunc.config.getoption("browser") 51 if browsers is not None: 52 metafunc.parametrize("browser", browsers, indirect=True, scope="class")
53
54 #noinspection PyShadowingNames,PyUnresolvedReferences 55 @pytest.fixture(scope='session') 56 -def browser(request, tlib_logger, adb_logger):
57 """ 58 Creates a browser as specified by the command line parameter --browser and --proxy\n 59 Note: Currently proxy parameter is ignored 60 """ 61 global b 62 63 #Don't create a browser object is --browser was not specified 64 if not hasattr(request, 'param') or request.param is None: 65 tlib_logger.warn("Parameter browser was not specified, skipping test") 66 pytest.skip("Parameter browser was not specified, skipping test") 67 else: 68 if request.param == "ie": 69 tlib_logger.debug("Creating webdriver for IE") 70 b = webdriver.Ie(webdriver_ie_executable()) 71 request.addfinalizer(b.quit) 72 elif request.param == "chrome": 73 tlib_logger.debug("Creating webdriver for Chrome") 74 b = webdriver.Chrome(webdriver_chrome_executable()) 75 request.addfinalizer(b.quit) 76 elif request.param == "firefox": 77 tlib_logger.debug("Creating webdriver for Firefox") 78 b = webdriver.Firefox() 79 request.addfinalizer(b.quit) 80 elif request.param == "android": 81 tlib_logger.debug("Creating webdriver for Android") 82 83 serial_id = request.config.getoption("serial_id") 84 if serial_id is None: 85 tlib_logger.warn("Parameter --serial_id was not given. Running test on default device") 86 87 #Start android server 88 AndroidHelper.setup_webdriver(tlib_logger, adb_logger, serial_id) 89 b = webdriver.Remote(command_executor='http://localhost:8080/wd/hub', desired_capabilities=webdriver.DesiredCapabilities.ANDROID) 90 91 def fin(): 92 b.quit 93 AndroidHelper.teardown_webdriver(tlib_logger, adb_logger, serial_id)
94 95 request.addfinalizer(fin) 96 97 else: 98 #noinspection PyUnresolvedReferences 99 tlib_logger.warn("{browser}s is not a valid browser".format(browser=request.param)) 100 pytest.skip("{browser}s is not a valid browser".format(browser=request.param)) 101 102 103 return b 104
105 #noinspection PyUnresolvedReferences 106 @pytest.fixture(scope="session") 107 -def base_url(request, tlib_logger):
108 """ 109 Returns value of --base_url command line parameter 110 """ 111 if request.config.getoption("base_url") is None: 112 #noinspection PyUnresolvedReferences 113 tlib_logger.warn("parameter --base_url was not specified") 114 pytest.skip("parameter --base_url was not specified") 115 return None 116 else: 117 return request.config.getoption("base_url")
118
119 #noinspection PyUnresolvedReferences 120 @pytest.fixture(scope="session") 121 -def mobile_base_url(request, tlib_logger):
122 """ 123 Returns value of --mobile_base_url command line parameter 124 """ 125 if request.config.getoption("mobile_base_url") is None: 126 #noinspection PyUnresolvedReferences 127 tlib_logger.warn("parameter --mobile_base_url was not specified") 128 pytest.fail("parameter --mobile_base_url was not specified") 129 return None 130 else: 131 return request.config.getoption("mobile_base_url")
132
133 #noinspection PyUnresolvedReferences 134 @pytest.fixture(scope="session") 135 -def proxy(request, tlib_logger):
136 """ 137 Returns value of --proxy command line parameter 138 """ 139 if request.config.getoption("proxy") is None: 140 tlib_logger.info("Parameter --proxy was not specified") 141 return None 142 else: 143 p = request.config.getoption("proxy") 144 global port 145 global ip 146 #noinspection PyBroadException 147 try: 148 (ip, port) = p.split(":") 149 except: 150 tlib_logger.fatal("Invalid value for --proxy parameter: {proxy}. Should be an IP and port like this 172.27.108.99:8080") 151 #noinspection PyUnresolvedReferences 152 pytest.exit("""Invalid value for --proxy parameter: {proxy} 153 Should be an IP and port like this 172.27.108.99:8080""") 154 155 #validate ip is valid 156 if not TestHelper.is_valid_ip(ip): 157 tlib_logger.fatal("Proxy server has an invalid IP address: {ip}".format(ip=ip)) 158 #noinspection PyUnresolvedReferences 159 pytest.exit("Proxy server has an invalid IP address: {ip}".format(ip=ip)) 160 161 #Validate port is an integer between 1 and 65535 162 #noinspection PyBroadException 163 try: 164 port = int(port, base=10) 165 except: 166 tlib_logger.fatal("Proxy server has an invalid port: (port)".format(port=port)) 167 #noinspection PyUnresolvedReferences 168 pytest.exit("Proxy server has an invalid port: (port)".format(port=port)) 169 170 if int(port) not in range(1, 65535, 1): 171 tlib_logger.fatal("Proxy server has an invalid port: (port)".format(port=port)) 172 #noinspection PyUnresolvedReferences 173 pytest.exit("Proxy server has an invalid port: (port)".format(port=port)) 174 175 return p
176