我有一个基于Selenium
和Cucumber
的{{1}}测试,当我尝试通过移动方法Spring Boot
来简化测试时,抛出异常(如下所示的错误)和父类中的setup()
。
如果我将这两种方法都剪切粘贴到closeBrowser()
文件中,则一切正常,并且测试也顺利通过。
不确定如何将这些通用方法移至另一个类以简化测试并支持可维护性和可扩展性。
我在Google周围搜索并找到了该SO链接(Cucumber class extending step definitions and hooks),但是它没有任何代码段,并且没有提供太多帮助。
请指导。
DemoApplicationTests.java
GoogleCalcStepDefinition.java
GoogleCalcStepDefinition.java
@RunWith(SpringRunner.class)
@SpringBootTest
public abstract class DemoApplicationTests {
private static final String CHROME_DRIVER_EXE = "drivers/chromedriver.exe";
private static final String WEBDRIVER_CHROME_DRIVER = "webdriver.chrome.driver";
private WebDriver driver;
private GoogleSearchPage googleSearchPage;
@Before //this uses cucumber.api.Before (equivalent of JUnit @BeforeClass)
public void setup() {
String filePath = ClassLoader.getSystemClassLoader().getResource(CHROME_DRIVER_EXE).getFile();
System.setProperty(WEBDRIVER_CHROME_DRIVER, filePath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
driver = new ChromeDriver(options);
googleSearchPage = PageFactory.initElements(driver, GoogleSearchPage.class);
}
@After //this uses cucumber.api.After (equivalent of JUnit @AfterClass)
public void closeBrowser() {
if (null != driver) {
driver.close();
driver.quit();
}
}
}
TestRunner.java
@Ignore
public class GoogleCalcStepDefinition extends DemoApplicationTests {
@Given("^I open Google$")
public void I_open_google() {
//Set implicit wait of 5 seconds and launch google
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://www.google.co.in");
}
@When("^I enter \"([^\"]*)\" in search textbox$")
public void I_enter_in_search_textbox(String additionTerms) {
googleSearchPage.searchBox.sendKeys(additionTerms); //passing 2+2 here
googleSearchPage.searchBtn.click();
}
@Then("^I should get result as \"([^\"]*)\"$")
public void I_should_get_correct_result(String expectedResult) {
String result = googleSearchPage.calculatorTextBox.getText();
assertEquals(result, expectedResult); //Verify that result of 2+2 is 4
}
}
GoogleSearchPage.java
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {"pretty", "json:target/cucumber-reports/cucumber.json"},
features = {"src/test/resources/features"})
public class TestRunner {
}
计算功能(源代码/测试/资源/功能)
public class GoogleSearchPage {
@FindBy(name = "q")
WebElement searchBox;
@FindBy(name = "btnK")
WebElement searchBtn;
@FindBy(id = "cwos")
WebElement calculatorTextBox;
}
pom.xml
Feature: Check addition in Google calculatorcontent
In order to verify that Google calculator work correctly
As a user of Google
I should be able to get correct addition result
Scenario: Addition
Given I open Google
When I enter "2+2" in search textbox
Then I should get result as "4"
错误(执行TestRunner时):
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>2.45.0</version>
<scope>test</scope>
</dependency>
答案 0 :(得分:0)
如果要在不同的步骤定义类之间共享WebDriver,请考虑将Singleton Holder pattern用于Web驱动程序。
您的步骤定义不应初始化Web驱动程序和您的测试套件,理想情况下,它应仅包含与实际测试/声明相关的代码,最好创建一些初始化Web驱动程序的Test Manager类(单个),并运行测试,加载属性,设置测试数据等所需的一切,而在步骤定义中,只需引用您的测试管理器即可。
黄瓜将在跑步者类的'glue'目录中指定的所有类中寻找步骤定义。因此,可以说您有一些测试登录页面和商店页面的测试方案,您可能会:
答案 1 :(得分:0)
您可以使用开源框架QAF,该框架可以满足驱动程序管理,资源管理和其他功能测试自动化需求。使用qaf后,您将发现所有与设置和拆卸相关的代码都是多余的,因为qaf会注意这些代码。您可以将更多精力集中在AUT的自动化功能上。请参阅answer to similar question。