首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打开浏览器&导航- Java Automated Testing Selenium

打开浏览器&导航- Java Automated Testing Selenium
EN

Stack Overflow用户
提问于 2020-09-10 15:44:54
回答 3查看 56关注 0票数 1
代码语言:javascript
复制
 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"

全局基类

代码语言:javascript
复制
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;
}

登录

代码语言:javascript
复制
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();
}

}

问题是,当运行这个功能时,我得到了下面的错误,我不能理解为什么会发生这种情况,因为它没有给我错误所在的帮助。

未定义的步骤:给定我打开Chrome

未定义的步骤:当我浏览网站时

未定义的步骤:然后我使用"user1“和"password1”登录网站

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-09-11 22:14:01

可能您的“胶水”与您的测试运行器或配置没有正确对齐。

尝试使用步骤定义的路径编辑"Run > Edit Configuration“中的"glue”选项。

或者,如果您使用的是测试运行器类,请确保获得以下内容:

代码语言:javascript
复制
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 {

}

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

票数 0
EN

Stack Overflow用户

发布于 2020-09-10 16:06:32

步骤的函数名称错误。您遵循的camelCase方法在黄瓜的情况下是无效的。您已经将@Given("^I open Chrome$")步骤函数名称写为i_open_chrome(),而不是iOpenChrome()。您可以使用Tidy Gherkin chrome扩展来生成步骤定义。

有效的示例步骤。

代码语言:javascript
复制
@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();
}
票数 0
EN

Stack Overflow用户

发布于 2020-09-10 20:33:02

您应该通过右键单击不键入代码行将cucumber要素文件转换为Stepdefenition文件

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63825075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档