Selenium:在前两个函数完成后执行第三个函数



我在Firefox中使用Selenium Webdriver来运行我的代码。我正在使用pytest,以便可以并行运行这些函数。我有三个主要函数:第一个和第二个函数返回一些东西,第三个函数使用前面函数中的数据我的问题是,在前两个函数完成后,我想用两个函数返回的数据执行第三个函数

代码本身实际上要复杂得多,但为了澄清,我制作了一个新代码来演示我的问题。我有两个函数,一个被命名为"test1",另一个被称为"test2"——这两个函数都并行运行并返回信息。我还有一个名为"test3"的第三个函数,用于在test1和test2都完成后处理它们返回的数据。在这个例子中,"comp"应该只是打印出一些文本。

这是代码:

import unittest
from time import sleep
from selenium import webdriver
# pytest -s -v tests.py        <----- I use to execute this script
# py.test -s tests.py -d --tx 2*popen//python=python2.7    <------- I     use this to run the tests in parallel
# For some reason program doesnt print in parallel mode. Although "-s" fixes that in the normal execution of pytest.

class TestParallel(unittest.TestCase):

def setUp(self):
self.browser = webdriver.Firefox(executable_path='./dependencies/geckodriver')

def test1(self):
browser = self.browser
browser.get('https://www.google.com/')
asd = browser.find_element_by_xpath("/html/body/div/div[3]/div[1]/div/div/div/div[1]/div[1]/a").text # returns "Gmail"
sleep(2)
print asd
return asd
def test2(self):
browser = self.browser
browser.get('https://www.google.com/')
asd2 = browser.find_element_by_xpath("/html/body/div[1]/div[3]/div[1]/div/div/div/div[1]/div[2]/a").text # returns "Images"
sleep(1)
print asd2
return asd2
def test3(self):
print "word from test1 is " + TestParallel.test1(self) + " and word from test2 is " + TestParallel.test2(self)

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

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

也许有人对我如何解决这个问题有想法/建议。非常感谢。

编辑

好的,这里有另一个想法:

  • 在test1和test2之前声明两个空的全局字符串变量跑步
  • 让test1和test2写入这两个变量
  • 进行测试3使用while循环等待,直到两个变量都包含某些内容或者直到经过特定的时间量。

    class TestParallel(unittest.TestCase):
    string_a = ""
    string_b = ""
    def setUp(self):
    self.browser = webdriver.Firefox(executable_path='./dependencies/geckodriver')
    def test1(self):
    [..]
    string_a = "result_a"
    def test2(self):
    [..]
    string_b = "result_b"
    def test3(self):
    counter = 0;
    while ("" in string_a && "" in string_b):
    sleep(0.1)
    counter = counter + 1
    if counter > 200:
    break
    print "word from test1 is " + string_a + " and word from test2 is " + string_b
    

嗯,也许:

def test3(self):
counter = 0;
while ("" in string_a && "" in string_b):
sleep(0.1)
counter = counter + 1
if counter > 1000:
break
print "word from test1 is " + string_b + " and word from test2 is " + string_b

test1test2看起来并不像测试。。。它们看起来应该是从测试中使用的页面返回数据的函数。我会做一些更像的事情

def get_thing1(self):
return self.browser.find_element_by_xpath("/html/body/div/div[3]/div[1]/div/div/div/div[1]/div[1]/a").text # returns "Gmail"
def get_thing2(self):
return self.browser.find_element_by_xpath("/html/body/div[1]/div[3]/div[1]/div/div/div/div[1]/div[2]/a").text # returns "Images"
def test1(self):
browser = self.browser
browser.get('https://www.google.com/')
print "word from test1 is " + get_thing1(self) + " and word from test2 is " + get_thing2(self)

这两个方法get_thing1get_thing2假设您在正确的页面上,它们所做的只是返回您要查找的内容。请把名字改成更具描述性的。。。我不确定它们到底返回了什么,所以我编了一些通用名称。

现在您只有运行的test1和两个从页面中提取内容的方法。您可以任意多次运行test1。。。平行地,等等…循环或任何你想要的。

额外注释:

  1. 您真的不想使用那么多级别的XPath和/或从HTML标记开始的XPath。它们非常脆弱(只要对HTML做一点更改,就可能崩溃(
  2. 您应该花一些时间来了解页面对象模型。您应该创建一个GoogleSearchResultsPage页面对象,并使get_thing1()get_thing2()位于该页面对象中。然后测试将从GoogleSearchPage前进到GoogleSearchResultsPage,然后调用这两个方法等从页面获取数据

最新更新