java - 打开浏览器和导航 - Java 自动化测试 Selenium
问题描述
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"
全局基类
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;
}
登录
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”登录网站
解决方案
您的步骤函数名称错误。您正在遵循对黄瓜无效的 camelCase 方法。您已将@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();
}
推荐阅读
- c# - 使用 LDAPS 身份验证时出现 LDAP 服务器不可用错误
- azure-devops - Azure DevOps Pipeline - 发布 API - 更新发布定义添加和删除模板化阶段
- node.js - 从服务器下载 Flutter pdf
- python - Print index of row and column in adjacency matrix
- linux - Extract all unique URL from log with only POST type of the request
- autodesk-forge - 是否可以在 DA4R 中使用外部 IFC 导出器?
- database - 更改表以使其锁定时间最短的理想方法
- javascript - 将标头发送到客户端 Expressjs 路由器后无法设置标头
- android - 谷歌助手在没有服务器连接的情况下集成到应用程序
- javascript - 为什么我在 POO 中的计时器不起作用?(javascript)