首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ruby | Selenium | Web测试循环

Ruby | Selenium | Web测试循环
EN

Stack Overflow用户
提问于 2019-10-10 03:15:41
回答 1查看 203关注 0票数 0

我正在尝试编写一些ruby来执行以下操作:

我正在测试一个网页,它有一个表单,一个搜索按钮,以及下面的搜索结果。

当我在表单中输入发票编号(例如123456 )并单击“搜索”按钮时,如果发票已成功保存,则它将显示在“搜索结果”部分下,如果没有,则会显示“未找到结果”。有时数据库需要几秒钟才能完成对发票的处理,所以我想做一个循环,每隔几秒钟按一次搜索按钮,直到找到发票,一旦满足条件,就做一些其他的事情。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-10 04:13:12

您可以设置一个while循环,一旦找到满足特定条件的WebElement,该循环就会中断。

代码语言:javascript
复制
# Keep track of whether or not invoice has been found
found = false

# First, attempt to locate the desired invoice
begin
    # click the search button
    driver.find_element(:name, "search_button").click

    # check if invoice exists, set found to true if it does
    expect(driver.find_element(some_locator_here).displayed?).to eql true
    found = true

rescue Selenium::WebDriver::Error::NoSuchElementError
    # catch NoSuchElementError if invoice does not exist, leave found as false
    puts("Element not found")

# if invoice is not found, continue trying to find it in a loop
while found == false do
    begin
        # click the search button
        driver.find_element(:name, "search_button").click

        # attempt to locate the invoice
        expect(driver.find_element(some_locator_here).displayed?).to eql true
        found = true

    rescue Selenium::WebDriver::Error::NoSuchElementError
        puts("Element not found")

这将单击search按钮并尝试在循环中定位所需的发票,如果所需的发票不存在,则捕获NoSuchElementError。您需要用:name, "invoice_name"之类的东西替换some_locator_here,这样您就可以找到您的发票,并确定是否已经找到它。希望这能对你有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58310841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档