打开浏览器并导航 – Java自动化测试Selenium

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

Open Browser & Navigate - Java Automated Testing Selenium

问题

Feature: 登录网站

# 使用Chrome登录
Scenario: 使用Chrome进行登录
Given 打开Chrome
When 浏览网站
Then 使用"user1"和"password1"登录网站

全局基类

public class base {

public WebDriver driver;
public Properties prop;
public ChromeOptions coptions;

public WebDriver initializeDriver() throws IOException
{

    String browserName= "chrome";
    System.out.println(browserName);
    String pathToDriver = "";

    if(browserName.equals("chrome"))
    {
        pathToDriver = "C:\\Repositories\\automationProject\\webDrivers\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", pathToDriver);
        coptions.addArguments("disable-infobars");
        coptions.addArguments("--start-maximized");
        driver= new ChromeDriver(coptions);
        // 在Chrome驱动中执行

    }

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    return driver;
}

登录

public class login extends base {

@Given("^打开Chrome$")
public void iOpenChrome() throws Throwable {
    driver = initializeDriver();
}

@When("^浏览网站$")
public void iBrowseToWebsite() {
    driver.get("https://www.website.com/");
}

@Then("^使用\"([^\"]*)\"和\"([^\"]*)\"登录网站$")
public void iLoginToWebsiteUsingAnd£(String username, String password) throws Throwable {
    driver.findElement(By.id("UserName")).sendKeys(username);
    driver.findElement(By.id("Password")).sendKeys(password);
    driver.findElement(By.id("btnLogin")).click();
}
}

问题是,当运行此功能时,我收到以下错误消息,我无法理解为什么会出现此错误,因为它没有告诉我错误出现在哪里。

未定义的步骤:Given 打开Chrome

未定义的步骤:When 浏览网站

未定义的步骤:Then 使用"user1"和"password1"登录网站
英文:
 Feature: Login to Website

  #Login in Chrome
  Scenario: Login using Chrome
  Given I open Chrome
  When I browse to Website
  Then I login to Website using "user1" and "password1"

Global Base Class

public class base {

public WebDriver driver;
public Properties prop;
public ChromeOptions coptions;

public WebDriver initializeDriver() throws IOException
{

    String browserName= "chrome";
    System.out.println(browserName);
    String pathToDriver = "";

    if(browserName.equals("chrome"))
    {
        pathToDriver = "C:\\Repositories\\automationProject\\webDrivers\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", pathToDriver);
        coptions.addArguments("disable-infobars");
        coptions.addArguments("--start-maximized");
        driver= new ChromeDriver(coptions);
        //execute in chrome driver

    }

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    return driver;
}

Login

public class login extends base {

@Given("^I open Chrome$")
public void iOpenChrome() throws Throwable {
    driver = initializeDriver();
}

@When("^I browse to Website$")
public void iBrowseToWebsite() {
    driver.get("https://www.website.com/");
}

@Then("^I login to Website using \"([^\"]*)\" and \"([^\"]*)\"£\"$")
public void iLoginToWebsiteUsingAnd£(String username, String password) throws Throwable {
    driver.findElement(By.id("UserName")).sendKeys(username);
    driver.findElement(By.id("Password")).sendKeys(password);
    driver.findElement(By.id("btnLogin")).click();
}

}

The issue is that when running this feature I am getting the below error which I cannot understand why this is happening as it gives me no help in where the error is.

Undefined step: Given I open Chrome

Undefined step: When I browse to Website

Undefined step: Then I login to Website using "user1" and "password1"

答案1

得分: 0

你的步骤函数名称不正确。你正在使用驼峰命名法,这在Cucumber的情况下是无效的。你应该将你的@Given("^I open Chrome$")步骤函数名称写成i_open_chrome(),而不是iOpenChrome()。你可以使用Tidy Gherkin Chrome 扩展来生成步骤定义。

有效的示例步骤:

@Given("^I open Chrome$")
public void i_open_chrome() throws Throwable {
    throw new PendingException();
}

@When("^I browse to Website$")
public void i_browse_to_website() throws Throwable {
    throw new PendingException();
}

@Then("^I login to Website using \"([^\"]*)\" and \"([^\"]*)\"$")
public void i_login_to_website_using_something_and_something(String strArg1, String strArg2) throws Throwable {
    throw new PendingException();
}
英文:

Your function name for the steps is wrong. You are following camelCase approach that is not valid in the case of cucumber. You have write your @Given("^I open Chrome$") step function name as i_open_chrome()instead of iOpenChrome(). You can use Tidy Gherkin chrome extension to generate step definitions.

Valid sample steps.

@Given("^I open Chrome$")
public void i_open_chrome() throws Throwable {
    throw new PendingException();
}

@When("^I browse to Website$")
public void i_browse_to_website() throws Throwable {
    throw new PendingException();
}

@Then("^I login to Website using \"([^\"]*)\" and \"([^\"]*)\"$")
public void i_login_to_website_using_something_and_something(String strArg1, String strArg2) throws Throwable {
    throw new PendingException();
}

答案2

得分: 0

你应该通过右键单击将黄瓜功能文件转换为Step Definition文件,不要键入代码行。

英文:

You should convert the cucumber feature file to the Stepdefenition file by right click don't type the line of code

答案3

得分: 0

可能你的“glue”与你的测试运行器或配置不正确对齐。

尝试在“运行 > 编辑配置”中编辑“glue”选项,将其路径设置为你的步骤定义所在的路径。

或者,如果你正在使用一个测试运行器类,请确保你得到了这样的代码:

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "路径\\到\\特性文件",
        glue = "stepdefs"
)
public class RunTest {

}

其中 stepdefs 是包含你的步骤定义文件的包名。

英文:

Probably your "glue" is not aligned correctly with your test runner or configuration.

Try editing the "glue" option in "Run > Edit Configuration" with the path to your step definitions.

Or, if you are using a test runner class, make sure you get this:

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "path\\to\\features",
        glue = "stepdefs"
)
public class RunTest {

}

Where stepdefs is a package name with your step definition file.

huangapple
  • 本文由 发表于 2020年9月10日 15:44:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63825075.html
匿名

发表评论

匿名网友

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

确定