黄瓜无法定位元素-第一个方案通过而后续方案失败

时间:2019-04-10 14:55:51

标签: selenium selenium-webdriver cucumber

enter image description here 我正在使用Selenium / TestNG运行黄瓜测试,并通过html报告。第一种情况可以通过,但随后的情况在通过时会失败,因为无法找到元素。第二种情况(一个失败)与第一种情况几乎相同,只是检查页面上是否显示了其他元素-确实是。这是我的功能文件的样子:

Feature: LoginFeature
  This deals with logging in

  Scenario: Log in with correct username

    Given I navigate to the login page
    And I enter the following login details:
      | username | password |
      | cukey    | passwoid |
    And I click the login button
    Then I should land on the newest page

  Scenario: Log in with correct username part 2

    Given I navigate to the login page
    And I enter the following login details:
      | username | password |
      | cukey    | passwoid |
    And I click the login button
    Then I should land on the newest page by other measure

  Scenario: Log in with correct username to fail

    Given I navigate to the login page
    And I enter the following login details:
      | username | password |
      | cukey    | passwoid |
    And I click the login button
    Then I should land on the newest page wrongly

这是我的步骤定义/实现(带有“中间名”的@then应该是应该通过的):

package Steps;

import Base.BaseUtil;
import Pages.LoginPageObjeks;
//import cucumber.api.DataTable;
import io.cucumber.datatable.DataTable;
import cucumber.api.PendingException;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.ArrayList;
import java.util.List;


public class MaStepdefs extends BaseUtil {
    private BaseUtil base;

    public MaStepdefs(BaseUtil base) {
        this.base = base;
    }

    @And("^I click the login button$")
    public void iClickTheLoginButton() throws Throwable {
        LoginPageObjeks page = new LoginPageObjeks(base.Driver);
        page.ClickLogin();

    }

    @Given("^I navigate to the login page$")
    public void iNavigateToTheLoginPage() throws Throwable {

        base.Driver.navigate().to("http://www.executeautomation.com/demosite/Login.html");

    }

    @And("^I enter the following login details:$")
    public void iEnterTheFollowingLoginDetails(DataTable table) throws Throwable {

        List<List<String>> data = table.asLists(String.class);
        String userName = data.get(1).get(1);
        String passWord = data.get(1).get(0);

        List<User> users = new ArrayList();
        users.add(new User(userName, passWord));

        LoginPageObjeks page = new LoginPageObjeks(base.Driver);

        for (User user : users)
            page.Login(user.username, user.password);


        //List<User> users = new ArrayList<User>();

        //users = table.asList(User.class);




            Thread.sleep(2000);


        }

        @Then("^I should land on the newest page$")
        public void iShouldLandOnTheNewestPage () throws Throwable {
            Assert.assertEquals("It's not displayed", base.Driver.findElement(By.id("Initial")).isDisplayed(), true);
        }


    @Then("^I should land on the newest page wrongly$")
    public void iShouldLandOnTheNewestPageWrongly() throws Throwable {
        Assert.assertEquals("It's not displayed", base.Driver.findElement(By.id("fish")).isDisplayed(), true);
    }

    @Then("^I should land on the newest page by other measure$")
    public void iShouldLandOnTheNewestPageByOtherMeasure() throws Throwable {
        Assert.assertEquals("It's not displayed", base.Driver.findElement(By.id("Middle Name")).isDisplayed(), true);
    }

}

    class User {
        public String username;
        public String password;

        public User(String userName, String passWord) {
            username = userName;
            password = passWord;
        }
    }

这是我的测试跑步者:

package Runner;


import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;

//@RunWith(Cucumber.class)
@CucumberOptions(features = {"src/test/java/features"}, plugin = {"json:target/cucumber.json", "html:target/site/cucumber-pretty"}, glue = "Steps")

public class TestRunner extends AbstractTestNGCucumberTests {
}

有人知道这是什么原因吗?我需要为TestNG添加一些内容以运行多个场景吗?任何帮助将不胜感激,谢谢

0 个答案:

没有答案