am trying to automate tests on nopcommerce website as a practice but after first and second step it stops

huangapple go评论104阅读模式
英文:

am trying to automate tests on nopcommerce website as a practice but after first and second step it stops

问题

我不知道为什么在选择性别后停止并显示为通过。它打开浏览器,然后导航到注册页面,然后选择性别,测试停止并显示为通过,而没有完成后续步骤。

可能的问题是在注册页面完成后,测试脚本没有等待足够长的时间来进行下一步。在每个步骤之后,您使用了Thread.sleep(1000);来等待,但这种等待方式可能不是最可靠的。建议尝试使用显式等待来等待页面元素的可见性或其他条件,以确保脚本在继续执行之前等待正确的条件。

您还可以在测试失败时输出日志或调试信息,以查看测试停止的原因。这可能有助于确定问题的根本原因。

另外,请确保您的测试数据和测试步骤没有错误,例如选择的性别是否正确,电子邮件地址是否有效等。

如果问题仍然存在,您可以提供更多的错误信息或日志,以便我可以更详细地帮助您找出问题所在。

英文:

> am trying to automate tests on https://demo.nopcommerce.com/ website as a practice but after first and second step it stops and says test passed without continuing to the next steps am using selenium and cucumber and testng
> am automating it on edge driver

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 "sabry@gmail.com"
And user fills password "123456" field and confirm it
And user click on register button
Then success message is displayed
Then user close browser

RegisterPage.java
```package pages;

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();
}
}```    

i don't know why it stops after selecting gender and says passed
it opens the browser and then navigates to register page
then it selects the gender
and the test stops saying it is passed without completing the steps

答案1

得分: 1

"Looks like you have an issue, because annotations in code and Gherkin spec are different."

"When in your code you are using When and And"

"Conditions from Gherkin spec and Definitions class should match."

英文:

Looks like you have an issue, because annotations in code and Gherkin spec are different.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

Given user navigate to register page
When user select gender &quot;female&quot;

<!-- end snippet -->

When in your code you are using When and And

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

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

<!-- end snippet -->

Conditions from Gherkin spec and Definitions class should match.

huangapple
  • 本文由 发表于 2023年7月28日 02:50:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782649.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定