【问题标题】:Storing a boolean result in a variable using the Python WebDriver for Pytest使用 Python WebDriver for Pytest 将布尔结果存储在变量中
【发布时间】:2014-11-17 23:45:37
【问题描述】:

我正在将我的测试用例从 Selenium IDE 导出到 Python 并使用脚本将它们从 Unittest 转换为 Py.test。在进行一些手动更改时,我偶然发现了一个我需要解决的问题。

在 IDE 中,我有一个场景,我在 WebUI 中访问一个页面,存储该网页内容,并验证是否存在某个驱动程序:

storeBodyText  |  myBody  |     
storeEval      |  javascript{storedVars['myBody'].search("driver-video");}  |  isVideo

从这里,我访问设置页面并检查某些选项是否存在。如果在上面的代码中找到了视频驱动程序,那么“设置”页面上应该存在该选项的链接。如果没有,那么链接将不会出现,我不需要验证它是否存在。我在 IDE 中按如下方式处理:

if  |  storedVars['isVideo']!=-1
       waitForText  |  link=Display  |  Display
endIf

然后我使用 'Python / unittest / WebDriver' 导出我的代码并运行我创建的转换脚本,它将 unittest 代码转换为 Py.test。这给我留下了以下相关代码:

myBody = driver.find_element_by_tag_name("BODY").text
# ERROR: Caught exception [ERROR: Unsupported command [getEval | javascript{storedVars['myBody'].search("driver-video");} | ]]
.
.
.
# ERROR: Caught exception [unknown command [if]]
WebDriverWait(driver, 60).until(
        expected_conditions.text_to_be_present_in_element((By.LINK_TEXT, "Display"), "Display")
    )
#         for i in range(60):
#             try:
#                 if "Display" == driver.find_element_by_link_text("Display").text: break
#             except: pass
#             time.sleep(1)
#         else: self.fail("time out")

# ERROR: Caught exception [unknown command [endIf]]

错误不是来自我的脚本。由于将测试用例导出到“Python / unittest / WebDriver”而存在错误。当然,完全可以预料的是,我使用 java 脚本来执行该操作。现在,我正在尝试将其转换为 Py.test 并执行与在 Selenium IDE 中相同的操作。我正在寻找一种方法...

  1. 存储搜索'driver-video'驱动程序的结果,这将是一些布尔值。如果可能的话,我想要一种方法,如果可能的话,将这个结果直接存储到一个变量中。可能看起来或接近于...的东西......

isVidPres = WebDriverWait(driver,60).until(
               assert re.search("^[\s\S]*driver-video[\s\S]*$",driver.find_element_by_css_selector("myBody").text
)
  1. 使用该布尔变量来确定我是否需要验证是否存在“显示”链接。

如有必要,我愿意使用 try/catch 循环,但如果可能,我希望在没有它的情况下执行此操作。有谁知道如何执行此操作?提前感谢任何可以帮助我的人。

【问题讨论】:

    标签: python selenium-webdriver selenium-ide pytest


    【解决方案1】:

    我能够自己找到解决此问题的方法。认为发布它可能是一个好主意,以防其他人在负责将 Selenese 代码导出到 Python/unittest/Web Driver 然后将其转换为 Py.test 代码。

    为了搜索页面以验证是否存在某些驱动程序,我使用了以下代码:

            systemPage = driver.find_element_by_css_selector("BODY").text
    
            if re.match("^[\s\S]*xorg-driver-video-intel[\s\S]*$", systemPage):
                isVideoPres = True
            else:
                isVideoPres = False
    

    驱动程序存储在一个表中,但不保证驱动程序在表中的位置[如果已安装]在同一位置。因此,我存储了页面的内容并执行了正则表达式搜索驱动程序的名称。

    如果安装了驱动程序,我验证相关链接是否存在的部分,我使用了以下代码:

            if isVideoPres == True:
                WebDriverWait(driver, 60).until(
                    expected_conditions.text_to_be_present_in_element((By.LINK_TEXT, "Display"), "Display")
                )
            elif isVideoPres == False:
                WebDriverWait(driver, 60).until(
                    expected_conditions.invisibility_of_element_located((By.LINK_TEXT, "Display"))
                )
    

    在这段代码中,我检查是否找到了视频驱动程序。如果是这样,我会验证将我带到“显示”设置的链接是否可供用户单击。如果没有找到驱动程序,那么我验证设置页面上的链接不存在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-26
      • 2016-07-13
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      相关资源
      最近更新 更多