我已经用Java在黄瓜中编写了一个功能文件以进行登录操作。 我想在启动应用程序后仅执行多次登录操作,而不关闭应用程序。
这里的功能文件
Feature: Login Scenario
Scenario Outline: Execute me multiple times
Given I open the application
When I enter `"<username>"` and `"<password>"`
Then I click on Login button
And I close the application
Examples:
| username | password |
| user1 | password1 |
| user2 | password2 |
| user3 | password3 |
| user4 | password4 |
| user5 | password5 |
在这种情况下,每次启动和关闭应用程序时。但是我只想启动和关闭一次,然后执行多个登录操作。
亚军类:
import cucumber.api.CucumberOptions;
@CucumberOptions(features="feature",glue= {"steps"})
public class TestRunner {}
和步骤定义:
public class TestMe {
WebDriver driver;
String chrome="webdriver.chrome.driver";
String path="./drivers/chromedriver1.exe";
@Given("^I open the application$")
public void i_open_the_application() throws Throwable {
System.setProperty(chrome, path);
driver=new ChromeDriver();
driver.get("file:///C:/Users/AZ/Desktop/webpages/loginPage.html");
}
@When("^I enter \"([^\"]*)\" and \"([^\"]*)\"$")
public void i_enter_and(String user, String pass) throws Throwable {
driver.findElement(By.id("user")).sendKeys(user);
driver.findElement(By.id("pass")).sendKeys(pass);
}
@Then("^I click on Login button$")
public void i_click_on_Login_button() throws Throwable {
driver.findElement(By.id("btn")).click();
}
@Then("^I close the application$")
public void i_close_the_application() throws Throwable {
driver.close();
}
}
网页HTML代码:
<html>
<head>
<title>Login</title>
</head>
<body>
<div align="center">
Username<input type="text" id="user"></br></br>
Password<input type="password" id="pass"></br></br>
<input type="button" value="Login" id="btn">
</div>
</body>
</html>
预期结果:仅启动和关闭一次,并多次登录操作。
实际结果:多次启动,登录操作并关闭。
答案 0 :(得分:0)
结果::以下方法只能启动和关闭浏览器一次,并尝试多次登录
重点区域::您应从方案大纲中排除浏览器的启动和关闭代码,因为我们将其保留在注释 @BeforeClass 和 @AfterClass 下strong>
Scenario Outline: Execute me multiple times
When I enter "<username>" and "<password>"
Then I click on Login button
Examples:
| username | password |
| user1 | password1 |
| user2 | password2 |
| user3 | password3 |
| user4 | password4 |
| user5 | password5 |
TestRunner.Java
@CucumberOptions(features = "feature",
glue = {"steps" },
plugin = { "pretty","json:target/cucumber-json/cucumber.json",
"junit:target/cucumber-reports/Cucumber.xml", "html:target/cucumber-reports"},
monochrome = true)
public class TestRunner {
WebDriver driver;
String chrome="webdriver.chrome.driver";
String path="./drivers/chromedriver1.exe";
@BeforeClass
void launchBrowser() {
System.setProperty(chrome, path);
driver=new ChromeDriver();
DriverManager.setWebDriver(driver);
driver.get("file:///C:/Users/AZ/Desktop/webpages/loginPage.html");
}
@AfterClass
void closeBrowser() {
driver.close();
}
}
DriverManager.Java
public class DriverManager {
public static ThreadLocal<WebDriver> dr = new ThreadLocal<WebDriver>();
public static WebDriver getDriver() {
return dr.get();
}
public static void setWebDriver(WebDriver driver) {
dr.set(driver);
}
}
StepDefinition.Java
class StepDefinition
{
WebDriver driver;
public StepDefinition() {
driver = DriverManager.get();
}
@When("^I enter \"([^\"]*)\" and \"([^\"]*)\"$")
public void i_enter_and(String user, String pass) throws Throwable {
driver
driver.findElement(By.id("user")).sendKeys(user);
driver.findElement(By.id("pass")).sendKeys(pass);
}
@Then("^I click on Login button$")
public void i_click_on_Login_button() throws Throwable {
driver.findElement(By.id("btn")).click();
}
}
答案 1 :(得分:0)
您必须将场景与数据表一起使用,才能多次重复同一步骤,而无需关闭浏览器。
因此,如下更改功能文件。
Feature: Login Scenario
Scenario: Execute me multiple times
Given I open the application
And validate the credentials
| username | password |
| user1 | password1 |
| user2 | password2 |
| user3 | password3 |
And I close the application
然后按如下所示实现stepdef。
@Given("validate the credentials:")
public void validate_the_credentials(List<String> animals) {
}
答案 2 :(得分:0)
package steps;
import java.util.Map;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.DataTable;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
public class Test {
WebDriver driver;
String gecko="webdriver.chrome.driver";
String path="./drivers/chromedriver1.exe";
@Given("^I open the application$")
public void i_open_the_application() throws Throwable {
System.setProperty(gecko, path);
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("file:///C:/Users/AZ/Desktop/webpages/loginPage.html");
}
@When("^I enter username and password$")
public void i_enter_username_and_password(DataTable table) throws Throwable {
for (Map<String,String> data : table.asMaps(String.class,String.class)) {
driver.findElement(By.id("user")).sendKeys(data.get("username"));
driver.findElement(By.id("pass")).sendKeys(data.get("password"));
driver.findElement(By.id("btn")).click();
Thread.sleep(3000);
driver.navigate().refresh();
Thread.sleep(3000);
}
}
@When("^I close the application$")
public void i_close_the_application() throws Throwable {
driver.close();
}
}