Selenium Hub,推出2个浏览器只运行其中一个测试(selenium python)

时间:2016-05-27 17:36:04

标签: python selenium automated-tests selenium-grid qa

TL / DR:现在它启动了2个浏览器,但只在1中运行测试。我缺少什么?

所以我试图让selenium hub在mac上工作(OS X 10.11.5)。我安装了this,然后在终端标签中启动集线器:

selenium-standalone start -- -role hub

然后在同一台机器上的另一个终端选项卡中注册一个节点。

selenium-standalone start -- -role node -hub http://localhost:4444/grid/register -port 5556

它显示在console中,包含5个可用的Firefox和Chrome浏览器。 enter image description here

所以这是我的代码。在名为globes.py的文件中,我有这个。

class globes:
    def __init__(self, number):
        self.number = number

    base_url = "https://fake-example.com"

    desired_cap = []
    desired_cap.append ({'browserName':'chrome', 'javascriptEnabled':'true', 'version':'', 'platform':'ANY'})
    desired_cap.append ({'browserName':'firefox', 'javascriptEnabled':'true', 'version':'', 'platform':'ANY'})
    selenium_server_url = 'http://127.0.0.1:4444/wd/hub'

现在我只想尝试运行一个看起来像这样的测试。

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from globes import *

class HeroCarousel(unittest.TestCase):

    def setUp(self):
        for driver_instance in globes.desired_cap:
            self.driver = webdriver.Remote(
                command_executor=globes.selenium_server_url,
                desired_capabilities=driver_instance)

        self.verificationErrors = []

    def test_hero_carousel(self):
        driver = self.driver
        driver.get(globes.base_url)

        hero_carousel = driver.find_element(By.CSS_SELECTOR, 'div.carousel-featured')
        try: self.assertTrue(hero_carousel.is_displayed())
        except AssertionError, e: self.verificationErrors.append("home_test: Hero Carousel was not visible")

    def tearDown(self):
        self.driver.close()
        self.assertEqual([], self.verificationErrors)


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

现在它启动了Firefox和Chrome,但只在Firefox中运行测试。 Chrome会打开,只是位于空白页面上而不会关闭。所以我认为我编写测试的方式有问题。那我错过了什么?我很抱歉,如果这是显而易见的,但我只是学习如何设置集线器,并且刚刚学会了足够的python来编写几周前的selenium测试。

我认为Hubs正在启动它们,但我确实尝试在不同端口上的同一台机器上添加第二个节点并得到相同的东西。以防万一这是集线器打印出来的。

INFO - Got a request to create a new session: Capabilities [{browserName=chrome, javascriptEnabled=true, version=, platform=ANY}]
INFO - Available nodes: [http://192.168.2.1:5557]
INFO - Trying to create a new session on node http://192.168.2.1:5557
INFO - Trying to create a new session on test slot {seleniumProtocol=WebDriver, browserName=chrome, maxInstances=5, platform=MAC}
INFO - Got a request to create a new session: Capabilities [{browserName=firefox, javascriptEnabled=true, version=, platform=ANY}]
INFO - Available nodes: [http://192.168.2.1:5557]
INFO - Trying to create a new session on node http://192.168.2.1:5557
INFO - Trying to create a new session on test slot {seleniumProtocol=WebDriver, browserName=firefox, maxInstances=5, platform=MAC}

2 个答案:

答案 0 :(得分:2)

请原谅我,如果我没有实际使用selenium,这个答案完全基于与setUp

中仅保留对最后创建的驱动程序的引用相关的问题

不是保留一个self.driver,而是需要列出所有驱动程序,让我们说self.drivers,然后在处理它们而不是driver = self.driver时,您会for driver in self.drivers:并将所有相关代码缩进到for循环中,如下所示:

class HeroCarousel(unittest.TestCase):

    def setUp(self):
        self.drivers = [] #could make this with list comprehension
        for driver_instance in globes.desired_cap:
            driver = webdriver.Remote(
                command_executor=globes.selenium_server_url,
                desired_capabilities=driver_instance)
            self.drivers.append(driver)

        self.verificationErrors = []

    def test_hero_carousel(self):
        for driver in self.drivers:
            driver.get(globes.base_url)

            hero_carousel = driver.find_element(By.CSS_SELECTOR, 'div.carousel-featured')
            try: self.assertTrue(hero_carousel.is_displayed())
            except AssertionError, e: self.verificationErrors.append("home_test: Hero Carousel was not visible")

    def tearDown(self):
        for driver in self.drivers:
            driver.close()
        self.assertEqual([], self.verificationErrors)

答案 1 :(得分:1)

您需要使用self.driver.quit(),否则浏览器将不会退出,只会关闭当前窗口。

您很快就会开始运行多个浏览器,您将需要为它们付费。