在这种情况下,当我尝试 运行 单个测试用例脚本时,为什么会打开多个浏览器实例?
Why are multiple Browser instances opening when I am trying to run a single Test Case script in this case?
我试图使用 Page Factory 模型和 Cucumber 自动执行一些示例测试场景,但似乎不明白为什么当我尝试 运行 单个测试用例时会出现多个驱动程序实例。
当我尝试 运行 仅测试用例 1 到 Runner.java 文件时
package execution;
import java.io.File;
import org.junit.AfterClass;
import org.junit.runner.RunWith;
import com.cucumber.listener.Reporter;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features = "D:\Eclipse Wokspace\EcommerceProject\Features\Test1.feature",
glue = { "stepDefinition" },
plugin = { "html:target/cucumber-html-report",
"pretty:target/cucumber-pretty.txt", "usage:target/cucumber-usage.json",
"junit:target/cucumber-results.xml","com.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/report.html" },
dryRun = false,
monochrome = true,
strict = true)
public class Runner {
@AfterClass
public static void writeExtentReport() {
Reporter.loadXMLConfig(
new File("D:\Eclipse Wokspace\EcommerceProject\src\test\resources\extent-config.xml"));
Reporter.setSystemInfo("User Name", System.getProperty("user.name"));
Reporter.setSystemInfo("Time Zone", System.getProperty("user.timezone"));
Reporter.setSystemInfo("Machine", "Windows 10 " + "64 Bit");
Reporter.setSystemInfo("Selenium", "3.7.0");
Reporter.setSystemInfo("Maven", "3.5.2");
Reporter.setSystemInfo("Java Version", "1.8.0_151");
}
}
其他两个步骤定义的@Before 和@After 类 似乎也被调用,从下面的日志中可见
2018-08-05 14:16:16,358 INFO [main] SameCostSteps:25 - ************* Test Case 2 Begin ***************
2018-08-05 14:16:31,264 INFO [main] HomePage:17 - PageFactory setup of HomePage
2018-08-05 14:16:31,321 DEBUG [main] HomePage:22 - Click on Mobile Link
2018-08-05 14:16:33,077 INFO [main] HomePage:24 - Navigating to Mobile Page
2018-08-05 14:16:33,083 INFO [main] MobilePage:35 - PageFactory setup of MobilePage
2018-08-05 14:16:33,123 INFO [main] CartLimitSteps:26 - ***************************************************************
2018-08-05 14:16:33,126 INFO [main] CartLimitSteps:27 - ******************** Test Case 3 Begin ************************
2018-08-05 14:16:33,127 INFO [main] CartLimitSteps:28 - ***************************************************************
2018-08-05 14:16:47,370 INFO [main] HomePage:17 - PageFactory setup of HomePage
2018-08-05 14:16:47,371 DEBUG [main] HomePage:22 - Click on Mobile Link
2018-08-05 14:16:48,771 INFO [main] HomePage:24 - Navigating to Mobile Page
2018-08-05 14:16:48,773 INFO [main] MobilePage:35 - PageFactory setup of MobilePage
2018-08-05 14:16:48,777 INFO [main] SortByNameSteps:24 - ************* Test Case 1 Begin ***************
2018-08-05 14:17:01,832 INFO [main] HomePage:17 - PageFactory setup of HomePage
2018-08-05 14:17:01,833 DEBUG [main] HomePage:22 - Click on Mobile Link
2018-08-05 14:17:03,563 INFO [main] HomePage:24 - Navigating to Mobile Page
2018-08-05 14:17:03,567 INFO [main] MobilePage:35 - PageFactory setup of MobilePage
2018-08-05 14:17:04,411 INFO [main] SortByNameSteps:40 - Retrieving initial unsorted product list
2018-08-05 14:17:04,411 INFO [main] MobilePage:47 - Collecting a list of Products
2018-08-05 14:17:04,872 INFO [main] MobilePage:41 - Selecting value from the dropdown list
2018-08-05 14:17:06,332 INFO [main] SortByNameSteps:48 - Retrieving final sorted list
2018-08-05 14:17:06,333 INFO [main] MobilePage:47 - Collecting a list of Products
2018-08-05 14:17:06,453 INFO [main] SortByNameSteps:51 - Sorting the unsorted array string by Name
2018-08-05 14:17:06,454 INFO [main] SortByNameSteps:53 - Verifying that the products have been sorted by Name
2018-08-05 14:17:06,454 INFO [main] MobilePage:58 - Verifying that the products have been sorted by Name
2018-08-05 14:17:06,459 INFO [main] SortByNameSteps:57 - ############# Test Case 1 Passed ###########
2018-08-05 14:17:06,463 INFO [main] SameCostSteps:33 - ************* Test Case 2 End ******************
2018-08-05 14:17:09,747 INFO [main] CartLimitSteps:37 - ***************************************************************
2018-08-05 14:17:09,749 INFO [main] CartLimitSteps:38 - ******************** Test Case 3 End ************************
2018-08-05 14:17:09,750 INFO [main] CartLimitSteps:39 - ***************************************************************
2018-08-05 14:17:09,770 INFO [main] SortByNameSteps:33 - ************* Test Case 1 End ***************
从上面的日志中可以看出,@Before 和 @After 正在对所有步骤定义 Classes 执行,即使是一个我 运行ning 只有 Test1.feature 文件。
我似乎不明白为什么会这样,因为它会导致大量浏览器实例不必要地打开。
其他类的代码如下:
页面工厂Classes:
A) HomePage.java
package pageFactory;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import Util.BasePage;
public class HomePage extends BasePage{
Logger log = Logger.getLogger(HomePage.class);
@FindBy(xpath="//*[@id=\"nav\"]/ol/li[1]/a")
WebElement mobileLink;
public HomePage() {
log.info("PageFactory setup of HomePage");
PageFactory.initElements(driver,this);
}
public MobilePage clickMobile() {
log.debug("Click on Mobile Link");
mobileLink.click();
log.info("Navigating to Mobile Page");
return new MobilePage();
}
}
B) MobilePage.java
package pageFactory;
import java.util.Arrays;
import java.util.List;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import Util.BasePage;
public class MobilePage extends BasePage {
Logger log = Logger.getLogger(MobilePage.class);
@FindBy(xpath = "//*[@id=\"top\"]/body/div/div/div[2]/div/div[2]/div[1]/div[3]/div[1]/div[1]/div/select")
WebElement SortBy;
@FindBy(xpath = "//h2[@class='product-name']/a")
List<WebElement> products;
@FindBy(xpath = "//*[@id=\"product-price-1\"]/span")
WebElement xperiaPrice;
@FindBy(id="product-collection-image-1")
WebElement xperiaImg;
@FindBy(xpath = "//*[@id=\"top\"]/body/div/div/div[2]/div/div[2]/div[1]/div[3]/ul/li[1]/div/div[3]/button")
WebElement addToCartBtn;
public MobilePage() {
log.info("PageFactory setup of MobilePage");
PageFactory.initElements(driver, this);
}
public void sortByIndex(int val) {
Select dlSort = new Select(SortBy);
log.info("Selecting value from the dropdown list");
dlSort.selectByIndex(val);
}
public String[] retrieveProductNames() {
log.info("Collecting a list of Products");
String[] productList = new String[3];
int i = 0;
for (WebElement el : products) {
productList[i] = el.getText();
i++;
}
return productList;
}
public void verifySorting(String[] array1, String[] array2) {
log.info("Verifying that the products have been sorted by Name");
Assert.assertTrue(Arrays.equals(array1, array2), "The products have not been sorted proerly");
}
public String getXperiaPrice() {
return xperiaPrice.getText();
}
public XperiaPage clickOnXperia() {
xperiaImg.click();
return new XperiaPage();
}
public ShoppingCartPage addToCart() {
addToCartBtn.click();
return new ShoppingCartPage();
}
}
C) XperiaPage.java
package pageFactory;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import Util.BasePage;
public class XperiaPage extends BasePage{
@FindBy(xpath = "//*[@id=\"product-price-1\"]/span")
WebElement xperiaPrice;
public XperiaPage() {
PageFactory.initElements(driver,this);
}
public String getXperiaPrice() {
return xperiaPrice.getText();
}
}
步骤定义文件
A) 测试用例 1
package stepDefinition;
import java.util.Arrays;
import org.apache.log4j.Logger;
import Util.BasePage;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import pageFactory.HomePage;
import pageFactory.MobilePage;
public class SortByNameSteps {
public HomePage objHomePage;
public MobilePage objMobilePage;
public Logger log = Logger.getLogger(SortByNameSteps.class);
public String[] unsorted;
public String[] sorted;
@Before
public void setup() {
log.info("************* Test Case 1 Begin ***************");
BasePage.initialization();
objHomePage = new HomePage();
objMobilePage = objHomePage.clickMobile();
}
@After
public void tearDown() {
log.info("************* Test Case 1 End ***************");
BasePage.closeSession();
}
@When("^Select Name from the DropDownList$")
public void select_Name_from_the_DropDownList() throws Throwable {
// Write code here that turns the phrase above into concrete actions
log.info("Retrieving initial unsorted product list");
unsorted = objMobilePage.retrieveProductNames();
objMobilePage.sortByIndex(1);
}
@Then("^Verify that the Products have been sorted by Name$")
public void verify_that_the_Products_have_been_sorted_by_Name() throws Throwable {
// Write code here that turns the phrase above into concrete actions
log.info("Retrieving final sorted list");
sorted = objMobilePage.retrieveProductNames();
log.info("Sorting the unsorted array string by Name");
Arrays.sort(unsorted);
log.info("Verifying that the products have been sorted by Name");
try {
objMobilePage.verifySorting(unsorted, sorted);
log.info("############# Test Case 1 Passed ###########");
}
catch (Exception ex) {
log.info("############# Test Case 1 Failed ###########");
}
}
}
B) 测试用例 2
package stepDefinition;
import org.apache.log4j.Logger;
import org.testng.Assert;
import Util.BasePage;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import pageFactory.HomePage;
import pageFactory.MobilePage;
import pageFactory.XperiaPage;
public class SameCostSteps {
public HomePage home;
public MobilePage mobile;
public XperiaPage xperia;
public String detailCost;
public String listCost;
public Logger log = Logger.getLogger(SameCostSteps.class);
@Before
public void tearUp() {
log.info("************* Test Case 2 Begin ***************");
BasePage.initialization();
home = new HomePage();
mobile = home.clickMobile();
}
@After
public void tearDown() {
log.info("************* Test Case 2 End ******************");
BasePage.closeSession();
}
@When("^Read the cost of Sony Xperia$")
public void read_the_cost_of_Sony_Xperia() throws Throwable {
// Write code here that turns the phrase above into concrete actions
log.info("Fetching cost of product on List Page");
listCost = mobile.getXperiaPrice();
}
@When("^Click on Sony Xperia Mobile$")
public void click_on_Sony_Xperia_Mobile() throws Throwable {
// Write code here that turns the phrase above into concrete actions
log.info("Clicking on Xperia");
xperia = mobile.clickOnXperia();
}
@When("^Read Sony Xperia Cost on Details Page$")
public void read_Sony_Xperia_Cost_on_Details_Page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
log.info("Fetching cost of product on Details Page");
detailCost = xperia.getXperiaPrice();
}
@Then("^Verify That both the prices are same$")
public void verify_That_both_the_prices_are_same() throws Throwable {
// Write code here that turns the phrase above into concrete actions
log.info("Verifying that the cost of products on both pages is same");
try {
Assert.assertEquals(detailCost, listCost);
log.info("############# Test Case 2 Passed ###########");
}
catch (Exception ex) {
log.info("Price of Xperia in List and Details Page is not same");
log.info("############# Test Case 2 Failed ###########");
}
}
}
基页Class
package Util;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class BasePage {
public static WebDriver driver = null;
public static Properties prop;
public static void initialization() {
System.setProperty("webdriver.gecko.driver", util.FFDRIVER_PATH);
driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(util.PAGELOAD_TIME, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(util.IMPICIT_WAIT, TimeUnit.SECONDS);
driver.get(util.URL);
}
public static void closeSession() {
driver.quit();
}
}
如评论中所述,问题是 Cucumber 正在执行 stepDefinition
包的各种 classes 中定义的所有 @Before
和 @After
方法。这也是预期的情况,因为黄瓜不知道要执行哪些方法,除非你专门告诉它。
一个简单的解决方案是创建不同的包来为每个测试用例保存不同的 classes 但正如 OP 正确指出的那样,对于像 100 个案例这样的情况,这不是一个可行的解决方案方法将限制用户无法从单个运行器执行所有功能文件 class.
解-
1) 对于这种特殊情况,使用 @BeforeClass
and @AfterClass
Junit 注释代替 @Before
和 @After
黄瓜注释。由于每个测试用例 Step Def 都有自己的 class,这应该适用于您的案例。
2) 也可以使用Tagged Hooks。它基本上是将钩子附加到场景中的名称。例如-
专题文件-
@Test1
Scenario: blah blah
@Test2
Scenario: blah blah 2
步骤定义 -
@Before("@Test1")
public void beforeFirst(){
//This will run only before the Scenario Test1
}
@Before("@Test2")
public void beforeSecond(){
//This will run only before the Scenario Test2
}
我试图使用 Page Factory 模型和 Cucumber 自动执行一些示例测试场景,但似乎不明白为什么当我尝试 运行 单个测试用例时会出现多个驱动程序实例。
当我尝试 运行 仅测试用例 1 到 Runner.java 文件时
package execution;
import java.io.File;
import org.junit.AfterClass;
import org.junit.runner.RunWith;
import com.cucumber.listener.Reporter;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features = "D:\Eclipse Wokspace\EcommerceProject\Features\Test1.feature",
glue = { "stepDefinition" },
plugin = { "html:target/cucumber-html-report",
"pretty:target/cucumber-pretty.txt", "usage:target/cucumber-usage.json",
"junit:target/cucumber-results.xml","com.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/report.html" },
dryRun = false,
monochrome = true,
strict = true)
public class Runner {
@AfterClass
public static void writeExtentReport() {
Reporter.loadXMLConfig(
new File("D:\Eclipse Wokspace\EcommerceProject\src\test\resources\extent-config.xml"));
Reporter.setSystemInfo("User Name", System.getProperty("user.name"));
Reporter.setSystemInfo("Time Zone", System.getProperty("user.timezone"));
Reporter.setSystemInfo("Machine", "Windows 10 " + "64 Bit");
Reporter.setSystemInfo("Selenium", "3.7.0");
Reporter.setSystemInfo("Maven", "3.5.2");
Reporter.setSystemInfo("Java Version", "1.8.0_151");
}
}
其他两个步骤定义的@Before 和@After 类 似乎也被调用,从下面的日志中可见
2018-08-05 14:16:16,358 INFO [main] SameCostSteps:25 - ************* Test Case 2 Begin ***************
2018-08-05 14:16:31,264 INFO [main] HomePage:17 - PageFactory setup of HomePage
2018-08-05 14:16:31,321 DEBUG [main] HomePage:22 - Click on Mobile Link
2018-08-05 14:16:33,077 INFO [main] HomePage:24 - Navigating to Mobile Page
2018-08-05 14:16:33,083 INFO [main] MobilePage:35 - PageFactory setup of MobilePage
2018-08-05 14:16:33,123 INFO [main] CartLimitSteps:26 - ***************************************************************
2018-08-05 14:16:33,126 INFO [main] CartLimitSteps:27 - ******************** Test Case 3 Begin ************************
2018-08-05 14:16:33,127 INFO [main] CartLimitSteps:28 - ***************************************************************
2018-08-05 14:16:47,370 INFO [main] HomePage:17 - PageFactory setup of HomePage
2018-08-05 14:16:47,371 DEBUG [main] HomePage:22 - Click on Mobile Link
2018-08-05 14:16:48,771 INFO [main] HomePage:24 - Navigating to Mobile Page
2018-08-05 14:16:48,773 INFO [main] MobilePage:35 - PageFactory setup of MobilePage
2018-08-05 14:16:48,777 INFO [main] SortByNameSteps:24 - ************* Test Case 1 Begin ***************
2018-08-05 14:17:01,832 INFO [main] HomePage:17 - PageFactory setup of HomePage
2018-08-05 14:17:01,833 DEBUG [main] HomePage:22 - Click on Mobile Link
2018-08-05 14:17:03,563 INFO [main] HomePage:24 - Navigating to Mobile Page
2018-08-05 14:17:03,567 INFO [main] MobilePage:35 - PageFactory setup of MobilePage
2018-08-05 14:17:04,411 INFO [main] SortByNameSteps:40 - Retrieving initial unsorted product list
2018-08-05 14:17:04,411 INFO [main] MobilePage:47 - Collecting a list of Products
2018-08-05 14:17:04,872 INFO [main] MobilePage:41 - Selecting value from the dropdown list
2018-08-05 14:17:06,332 INFO [main] SortByNameSteps:48 - Retrieving final sorted list
2018-08-05 14:17:06,333 INFO [main] MobilePage:47 - Collecting a list of Products
2018-08-05 14:17:06,453 INFO [main] SortByNameSteps:51 - Sorting the unsorted array string by Name
2018-08-05 14:17:06,454 INFO [main] SortByNameSteps:53 - Verifying that the products have been sorted by Name
2018-08-05 14:17:06,454 INFO [main] MobilePage:58 - Verifying that the products have been sorted by Name
2018-08-05 14:17:06,459 INFO [main] SortByNameSteps:57 - ############# Test Case 1 Passed ###########
2018-08-05 14:17:06,463 INFO [main] SameCostSteps:33 - ************* Test Case 2 End ******************
2018-08-05 14:17:09,747 INFO [main] CartLimitSteps:37 - ***************************************************************
2018-08-05 14:17:09,749 INFO [main] CartLimitSteps:38 - ******************** Test Case 3 End ************************
2018-08-05 14:17:09,750 INFO [main] CartLimitSteps:39 - ***************************************************************
2018-08-05 14:17:09,770 INFO [main] SortByNameSteps:33 - ************* Test Case 1 End ***************
从上面的日志中可以看出,@Before 和 @After 正在对所有步骤定义 Classes 执行,即使是一个我 运行ning 只有 Test1.feature 文件。
我似乎不明白为什么会这样,因为它会导致大量浏览器实例不必要地打开。
其他类的代码如下:
页面工厂Classes:
A) HomePage.java
package pageFactory;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import Util.BasePage;
public class HomePage extends BasePage{
Logger log = Logger.getLogger(HomePage.class);
@FindBy(xpath="//*[@id=\"nav\"]/ol/li[1]/a")
WebElement mobileLink;
public HomePage() {
log.info("PageFactory setup of HomePage");
PageFactory.initElements(driver,this);
}
public MobilePage clickMobile() {
log.debug("Click on Mobile Link");
mobileLink.click();
log.info("Navigating to Mobile Page");
return new MobilePage();
}
}
B) MobilePage.java
package pageFactory;
import java.util.Arrays;
import java.util.List;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import Util.BasePage;
public class MobilePage extends BasePage {
Logger log = Logger.getLogger(MobilePage.class);
@FindBy(xpath = "//*[@id=\"top\"]/body/div/div/div[2]/div/div[2]/div[1]/div[3]/div[1]/div[1]/div/select")
WebElement SortBy;
@FindBy(xpath = "//h2[@class='product-name']/a")
List<WebElement> products;
@FindBy(xpath = "//*[@id=\"product-price-1\"]/span")
WebElement xperiaPrice;
@FindBy(id="product-collection-image-1")
WebElement xperiaImg;
@FindBy(xpath = "//*[@id=\"top\"]/body/div/div/div[2]/div/div[2]/div[1]/div[3]/ul/li[1]/div/div[3]/button")
WebElement addToCartBtn;
public MobilePage() {
log.info("PageFactory setup of MobilePage");
PageFactory.initElements(driver, this);
}
public void sortByIndex(int val) {
Select dlSort = new Select(SortBy);
log.info("Selecting value from the dropdown list");
dlSort.selectByIndex(val);
}
public String[] retrieveProductNames() {
log.info("Collecting a list of Products");
String[] productList = new String[3];
int i = 0;
for (WebElement el : products) {
productList[i] = el.getText();
i++;
}
return productList;
}
public void verifySorting(String[] array1, String[] array2) {
log.info("Verifying that the products have been sorted by Name");
Assert.assertTrue(Arrays.equals(array1, array2), "The products have not been sorted proerly");
}
public String getXperiaPrice() {
return xperiaPrice.getText();
}
public XperiaPage clickOnXperia() {
xperiaImg.click();
return new XperiaPage();
}
public ShoppingCartPage addToCart() {
addToCartBtn.click();
return new ShoppingCartPage();
}
}
C) XperiaPage.java
package pageFactory;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import Util.BasePage;
public class XperiaPage extends BasePage{
@FindBy(xpath = "//*[@id=\"product-price-1\"]/span")
WebElement xperiaPrice;
public XperiaPage() {
PageFactory.initElements(driver,this);
}
public String getXperiaPrice() {
return xperiaPrice.getText();
}
}
步骤定义文件
A) 测试用例 1
package stepDefinition;
import java.util.Arrays;
import org.apache.log4j.Logger;
import Util.BasePage;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import pageFactory.HomePage;
import pageFactory.MobilePage;
public class SortByNameSteps {
public HomePage objHomePage;
public MobilePage objMobilePage;
public Logger log = Logger.getLogger(SortByNameSteps.class);
public String[] unsorted;
public String[] sorted;
@Before
public void setup() {
log.info("************* Test Case 1 Begin ***************");
BasePage.initialization();
objHomePage = new HomePage();
objMobilePage = objHomePage.clickMobile();
}
@After
public void tearDown() {
log.info("************* Test Case 1 End ***************");
BasePage.closeSession();
}
@When("^Select Name from the DropDownList$")
public void select_Name_from_the_DropDownList() throws Throwable {
// Write code here that turns the phrase above into concrete actions
log.info("Retrieving initial unsorted product list");
unsorted = objMobilePage.retrieveProductNames();
objMobilePage.sortByIndex(1);
}
@Then("^Verify that the Products have been sorted by Name$")
public void verify_that_the_Products_have_been_sorted_by_Name() throws Throwable {
// Write code here that turns the phrase above into concrete actions
log.info("Retrieving final sorted list");
sorted = objMobilePage.retrieveProductNames();
log.info("Sorting the unsorted array string by Name");
Arrays.sort(unsorted);
log.info("Verifying that the products have been sorted by Name");
try {
objMobilePage.verifySorting(unsorted, sorted);
log.info("############# Test Case 1 Passed ###########");
}
catch (Exception ex) {
log.info("############# Test Case 1 Failed ###########");
}
}
}
B) 测试用例 2
package stepDefinition;
import org.apache.log4j.Logger;
import org.testng.Assert;
import Util.BasePage;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import pageFactory.HomePage;
import pageFactory.MobilePage;
import pageFactory.XperiaPage;
public class SameCostSteps {
public HomePage home;
public MobilePage mobile;
public XperiaPage xperia;
public String detailCost;
public String listCost;
public Logger log = Logger.getLogger(SameCostSteps.class);
@Before
public void tearUp() {
log.info("************* Test Case 2 Begin ***************");
BasePage.initialization();
home = new HomePage();
mobile = home.clickMobile();
}
@After
public void tearDown() {
log.info("************* Test Case 2 End ******************");
BasePage.closeSession();
}
@When("^Read the cost of Sony Xperia$")
public void read_the_cost_of_Sony_Xperia() throws Throwable {
// Write code here that turns the phrase above into concrete actions
log.info("Fetching cost of product on List Page");
listCost = mobile.getXperiaPrice();
}
@When("^Click on Sony Xperia Mobile$")
public void click_on_Sony_Xperia_Mobile() throws Throwable {
// Write code here that turns the phrase above into concrete actions
log.info("Clicking on Xperia");
xperia = mobile.clickOnXperia();
}
@When("^Read Sony Xperia Cost on Details Page$")
public void read_Sony_Xperia_Cost_on_Details_Page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
log.info("Fetching cost of product on Details Page");
detailCost = xperia.getXperiaPrice();
}
@Then("^Verify That both the prices are same$")
public void verify_That_both_the_prices_are_same() throws Throwable {
// Write code here that turns the phrase above into concrete actions
log.info("Verifying that the cost of products on both pages is same");
try {
Assert.assertEquals(detailCost, listCost);
log.info("############# Test Case 2 Passed ###########");
}
catch (Exception ex) {
log.info("Price of Xperia in List and Details Page is not same");
log.info("############# Test Case 2 Failed ###########");
}
}
}
基页Class
package Util;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class BasePage {
public static WebDriver driver = null;
public static Properties prop;
public static void initialization() {
System.setProperty("webdriver.gecko.driver", util.FFDRIVER_PATH);
driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(util.PAGELOAD_TIME, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(util.IMPICIT_WAIT, TimeUnit.SECONDS);
driver.get(util.URL);
}
public static void closeSession() {
driver.quit();
}
}
如评论中所述,问题是 Cucumber 正在执行 stepDefinition
包的各种 classes 中定义的所有 @Before
和 @After
方法。这也是预期的情况,因为黄瓜不知道要执行哪些方法,除非你专门告诉它。
一个简单的解决方案是创建不同的包来为每个测试用例保存不同的 classes 但正如 OP 正确指出的那样,对于像 100 个案例这样的情况,这不是一个可行的解决方案方法将限制用户无法从单个运行器执行所有功能文件 class.
解-
1) 对于这种特殊情况,使用 @BeforeClass
and @AfterClass
Junit 注释代替 @Before
和 @After
黄瓜注释。由于每个测试用例 Step Def 都有自己的 class,这应该适用于您的案例。
2) 也可以使用Tagged Hooks。它基本上是将钩子附加到场景中的名称。例如-
专题文件-
@Test1
Scenario: blah blah
@Test2
Scenario: blah blah 2
步骤定义 -
@Before("@Test1")
public void beforeFirst(){
//This will run only before the Scenario Test1
}
@Before("@Test2")
public void beforeSecond(){
//This will run only before the Scenario Test2
}