在数据表中传递参数 Selenium/Cucumber/Java

Passing parameters in Data Tables Selenium/Cucumber/Java

我使用 POM 模型并将 Cucumber 用于我的自动化。

我正在尝试为登录实施一个消极的场景,并且我使用了以下策略。

我需要知道这是正确的方法还是我把它搞砸了。

我正在使用 xpath 断言登录。

login.feature

    Given User navigates to Site
    And User enters a "<Username>" username
    And User enters a "<Password>" password
    When User clicks on the login button
    Then User should see the failure "<message>"

    Examples:
      | Username | Password   | message|
      | User | Pwd  | //DIV[@class=''][text()='Your login name or password is incorrect.']/../..] |

login.steps

    @Then("^User should see the failure \"([^\"]*)\"$")
    public void user_should_see_the_failure(String arg1 ) throws Throwable {
login_page.assertLoginFailure(arg1);
    }


login.page

    public @FindBy(xpath = "//DIV[@class=''][text()='Your login name or password is incorrect.']/../..")
    WebElement assert_LoginFailure;


    public login_page assertLoginFailure(String arg1) throws Exception {
        Thread.sleep(5000);
        org.testng.Assert.assertEquals(assert_LoginFailure,arg1);
        return new login_page();


    }

DataTable中message下的值应该是登录不成功时预期的明文。理想情况下不应包含任何 xpath 或任何选择器。

选择器应在页面对象中定义。此外,选择器应修改为不包含文本本身。

这样当测试失败时,您将得到 NoSuchElementException 而不是 AssertionError。无法判断测试失败是由于无效凭据证明有效还是网站上的消息已更改。

Thread.sleep() 有点不受欢迎。而是查看隐式或显式等待。参考这个 - http://toolsqa.com/selenium-webdriver/implicit-explicit-n-fluent-wait/

这是你的主张:

org.testng.Assert.assertEquals(assert_LoginFailure,arg1);  

其中 assert_LoginFailure 是一个 网络元素 arg1 是一个 字符串。现在将字符串与 Web 元素进行比较没有任何意义,对吗?

您必须提取 文本 web 元素 中的内容,例如:

assert_LoginFailure.getText()  

您的断言将如下所示:

org.testng.Assert.assertEquals(assert_LoginFailure.getText(),arg1);  

希望这会有所帮助。