我尝试在 nopcommerce 网站上进行自动化测试作为一种实践,但在第一步和第二步之后它停止了

问题描述 投票:0回答:1

我正在尝试在 https://demo.nopcommerce.com/ 网站上进行自动化测试作为一种实践,但在第一步和第二步之后它停止并表示测试已通过,而没有继续执行下一步我使用 selenium 和 cucumber 和 testng 正在边缘驱动程序上实现自动化

Reigster.feature file
Feature: user register new account successfully.

  Scenario: user register with valid data
Given user open browser
Given user navigate to register page
When user select gender "female"
And user enter firstname "Mohamed" and lastname "Sabry"
And user enter valid email "[email protected]"
And user fills password "123456" field and confirm it
And user click on register button
Then success message is displayed
Then user close browser

注册页面.java ```包页面;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import org.testng.asserts.SoftAssert;

public class RegisterPage {

WebDriver driver;

SoftAssert soft=new SoftAssert();

public RegisterPage(WebDriver driver){
    this.driver=driver;
    PageFactory.initElements(driver,this);
}

public WebElement navigateToRegister(){


    return driver.findElement(By.xpath("/html/body/div[6]/div[1]/div[1]/div[2]/div[1]/ul/li[1]/a"));
}

public WebElement genderMale(){

    return driver.findElement(By.xpath("//*[@id=\"gender-male\"]"));
}
public WebElement genderFemale(){

    return driver.findElement(By.xpath("//*[@id=\"gender-female\"]"));
}

public WebElement firstName(){


    return driver.findElement(By.xpath("//*[@id=\"FirstName\"]"));
}

public WebElement lastName(){
    return driver.findElement(By.xpath("//*[@id=\"LastName\"]"));
}

public WebElement email(){
    return driver.findElement(By.xpath("//*[@id=\"Email\"]"));
}

public WebElement password(){
    return driver.findElement(By.xpath("//*[@id=\"Password\"]"));
}

public WebElement confirmPassword(){
    return driver.findElement(By.xpath("//*[@id=\"ConfirmPassword\"]"));
}

    public void registerButton(){
        driver.findElement(By.xpath("//*[@id=\"register-button\"]")).click();
    }

    public void result(){
        String expectedResult="//*[@id=\"register-button\"]";
        String actualResult=driver.findElement(By.xpath("/html/body/div[6]/div[3]/div/div/div/div[2]/div[1]")).getText();
        soft.assertEquals(actualResult,expectedResult);

    }

}```    

(RegisterStepDef.java)

```package stepDefenations;

import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;

import pages.RegisterPage;

public class RegisterStepDef {

WebDriver driver=null;
RegisterPage register;

@Given("user open browser")
public void user_open_browser() throws InterruptedException {
    String edgeDriverPath="C:\\Users\\engmo\\IdeaProjects\\AutomationProject\\drivers\\msedgedriver.exe";
    System.setProperty("webdriver.edge.driver",edgeDriverPath);
    driver=new EdgeDriver();
    driver.navigate().to("https://demo.nopcommerce.com/");
    driver.manage().window().maximize();
    Thread.sleep(2000);
    register=new RegisterPage(driver);
}

@When("user navigate to register page")
public void user_navigate_to_register_page() throws InterruptedException {

    register.navigateToRegister().click();
    Thread.sleep(1000);
}

@And("user select gender {string}")
public void user_select_gender(String gender) throws InterruptedException {
    if(gender.equals("male")) {
        register.genderMale().click();
    } else {
        register.genderFemale().click();
    }
    Thread.sleep(1000);

}

@And("user enter firstname {string} and lastname {string}")
public void user_enter_firstname_and_lastname(String first_name, String last_name)  {
    register.firstName().sendKeys(first_name);
    register.lastName().sendKeys(last_name);

}

@And("user enter valid email {string}")
public void user_enter_valid_email(String email) throws InterruptedException {
    register.email().sendKeys(email);
    Thread.sleep(1000);
}

@And("user fills password {string} field and confirm it")
public void user_fills_password_field_and_confirm_it(String password) throws InterruptedException {
    register.password().sendKeys(password);
    register.confirmPassword().sendKeys(password);
    Thread.sleep(1000);
}

@And("user click on register button")
public void user_click_on_register_button() throws InterruptedException {
    register.registerButton();
    Thread.sleep(1000);
}

@And("success message is displayed")
public void success_message_is_displayed() throws InterruptedException {
    register.result();
    Thread.sleep(1000);
}

@Then("user close browser")
public void user_close_browser() throws InterruptedException {
    Thread.sleep(3000);
    driver.quit();
}
}```    

我不知道为什么选择性别后就停止并说通过 它打开浏览器,然后导航到注册页面 然后它选择性别 并且测试停止说它已通过而未完成步骤

selenium-webdriver testing automated-tests cucumber testng
1个回答
0
投票

看起来你有问题,因为代码中的注释和 Gherkin 规范不同。

Given user navigate to register page
When user select gender "female"

在代码中使用

When
And

@When("user navigate to register page")
public void user_navigate_to_register_page() throws InterruptedException { ... }

@And("user select gender {string}")
public void user_select_gender(String gender) throws InterruptedException { ... }

Gherkin 规范和定义类的条件应匹配。

© www.soinside.com 2019 - 2024. All rights reserved.