我正在尝试使用Firefox驱动程序在Eclipse中运行脚本,并使用Cucumber Gherkin格式的BDD运行Selenium。当我在 Junit 上运行它时,我收到了很多异常,这是我对 Java 文件的代码如下。
注释.java
package annotation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import cucumber.annotation.en.Given;
import cucumber.annotation.en.Then;
import cucumber.annotation.en.When;
public class annotation {
WebDriver driver = null;
@Given("^I am on Facebook login page$")
public void goToFacebook() {
driver = new FirefoxDriver();
driver.navigate().to("https://www.facebook.com/");
}
@When("^I enter username as "(.*)"$")
public void enterUsername(String arg1) {
driver.findElement(By.id("email")).sendKeys(arg1);
}
@When ("^I enter password as "(.*)"$")
public void enterPassword(String arg1) {
driver.findElement(By.id("pass")).sendKeys(arg1);
driver.findElement(By.id("u_0_v")).click();
}
@Then("^Login should fail$")
public void checkFail() {
if(driver.getCurrentUrl().equalsIgnoreCase(
"https://www.facebook.com/login.php?login_attempt=1&lwv=110")){
System.out.println("Test1 Pass");
} else {
System.out.println("Test1 Failed");
}
driver.close();
}
@Then("^Relogin option should be available$")
public void checkRelogin() {
if(driver.getCurrentUrl().equalsIgnoreCase(
"https://www.facebook.com/login.php?login_attempt=1&lwv=110")){
System.out.println("Test2 Pass");
} else {
System.out.println("Test2 Failed");
}
driver.close();
}
}
这些是我在执行测试后收到的异常
功能:注释
#This 是如何使用背景来消除重复步骤 背景:[
90m# 注释\大纲.功能:4[0m 用户导航到脸书给定 我在脸书登录页面#Scenario 与 AND 场景:[90m# 注释\轮廓特征:9[0m [90m当 [0m[90mI 输入用户名为 "[0m[90m[1mTOM[0m[90m"[0m [90m# annotation.enterUsername(String)[0m [1A [31mWhen [0m[31mI 输入用户名为"[0m[31m[1mTOM[0m[31m"[0m [90m# annotation.enter用户名(String)[0m [31mjava.lang.NullPointerExceptionat annotation.annotation.enterUsername(annotation.java:22) 在✽.当我输入用户名为"TOM"(注释\大纲.功能:10) [0米 [90m和 [0m[90mI 输入密码为 "[0m[90m[1mJERRY[0m[90m"[0m [90m# annotation.enterPassword(String)[0m [1A [36mAnd [0m[36mI enter 密码为 "[0m[36m[1mJERRY[0m[36m"[0m [90m# annotation.enterPassword(String)[0m [90mThen [0m[90mLogin should Fail[0m [90m# annotation.checkFail()[0m [1A [36mThen [0m[36mLogin Should fail[0m [90m# annotation.checkFail()[0m
#This 是如何使用背景来消除重复步骤 背景:[
90m# 注释\大纲.功能:4[0m 用户导航到脸书给定 我在脸书登录页面#Scenario BUT 场景:[90m# 注释\轮廓特征:15[0m [90m当 [0m[90mI 输入用户名为 "[0m[90m[1mTOM[0m[90m"[0m [90m# annotation.enterUsername(String)[0m [1A [31mWhen [0m[31mI输入用户名为"[0m[31m[1mTOM[0m[31m"[0m [90m# annotation.enter用户名(String)[0m [31mjava.lang.NullPointerException at annotation.annotation.enterUsername(annotation.java:22) 在✽.当我输入用户名为"TOM"(注释\大纲.功能:16) [0米 [90m和 [0m[90mI 输入密码为 "[0m[90m[1mJERRY[0m[90m"[0m [90m# annotation.enterPassword(String)[0m [1A [36mAnd [0m[36mI enter 密码为 "[0m[36m[1mJERRY[0m[36m"[0m [90m# annotation.enterPassword(String)[0m [90mThen [0m[90mLogin should Fail[0m [90m# annotation.checkFail()[0m [1A [36mThen [0m[36mLogin Should fail[0m [90m# annotation.checkFail()[0m [90m但是 [0m[90m重新登录选项应该可用[0m [90m# 注释.checkRelogin()[0m [1A [36mBut [0m[36m重新登录选项 应该可用[0m [90m# annotation.checkRelogin()[0m
java.lang.NullPointerException at annotation.annotation.enterUsername(annotation.java:22) at ✽.When I输入用户名为"TOM"(注释\大纲功能:10)
java.lang.NullPointerException at annotation.annotation.enterUsername(annotation.java:22) at ✽.When I 输入用户名为"TOM"(注释\大纲.功能:16)
这是我的大纲功能文件
Feature: annotation
#This is how background can be used to eliminate duplicate steps
Background:
User navigates to Facebook Given
I am on Facebook login page
#Scenario with AND
Scenario:
When I enter username as "TOM"
And I enter password as "JERRY"
Then Login should fail
#Scenario with BUT
Scenario:
When I enter username as "TOM"
And I enter password as "JERRY"
Then Login should fail
But Relogin option should be available
"Given" 应该是 Background 步骤中该行的第一个关键字,如下所示:
Background: User navigates to Facebook
Given I am on Facebook login page
我能够在我的系统中运行您的场景。你能分享你正在使用的依赖项/jar 吗? 此外,然后语句中的 driver.close() 方法(登录应该失败)对于第一种情况可以正常工作,但在第二种情况下,它会在下一个Then(重新登录选项应该可用)之前关闭浏览器,statement.so 您可能想要删除该关闭方法。