【发布时间】:2020-04-21 13:02:07
【问题描述】:
我是 Selenium Webdriver 与 Cucumber 集成的新手,我无法成功登录。
这是我的功能文件:
Feature: Validate user can login successfully
@tag
Scenario: Successful login
Given User is on the Login screen
When User enters UserID and Password
And User clicks Login button
Then Home Page is successfully displayed
这是我的步骤定义:
public class Steps
{
WebDriver driver;
@Given("^User is on the Login screen$")
public void User_is_on_the_Login_screen() throws Throwable
{
System.out.println("Pumasok dito");
//OPEN THE BROWSER
System.setProperty("webdriver.chrome.driver", "C:\\Users\\I689629\\eclipse-workspace\\uat-core\\src\\test\\resources\\drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://go/eruat8");
throw new PendingException();
}
@When("^User enters \"(.*)\" and \"(.*)\"$")
public void User_enters_UserID_and_Password() throws Throwable
{
//ENTER THE FID
driver.findElement(By.id("txtUserDefault")).sendKeys("User123");
//driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//ENTER THE PASSWORD
driver.findElement(By.id("txtPassDefault")).sendKeys("Password123");
}
@And("^User clicks Login button$")
public void User_clicks_Login_button() throws Throwable
{
//CLICK THE LOGIN BUTTON
driver.findElement(By.xpath("//*[@id=\"frmDefault\"]/div[1]/div[2]/div[2]/div[2]/input")).click();
//}
}
@Then("^Home Page is successfully displayed$")
public void Home_Page_is_successfully_displayed() throws Throwable
{
System.out.println("Home page is displayed");
}
}
这是我的测试运行器:
@RunWith(Cucumber.class)
@CucumberOptions(plugin = { "pretty", "html:target/cucumber-html-report", "json:target/cucumber.json" },
features = {"src/test/resources/features" }, glue = { "StepDefinition" })
public class TestRunner
{
}
谁能帮帮我。谢谢。
【问题讨论】:
-
请在方法外将
private WebDriver driver;声明为类变量。然后你应该能够在同一个类的其他步骤中访问驱动程序。