大家好,在这篇文章中,我会向你解释一个移动测试自动化框架,它支持与TestNG和Appium以及Cucumber一起使用TestNG和Appium的Page Object Model。 在这个框架中,您可以使用任何方法编写移动测试自动化测试并且并行运行它们。 在开始这篇文章之前,我强烈建议你从下面的appium文章开始。 我将扩展无线设备实验室架构,我将在本文中添加一个appiumCucumber 支持。
Cucumber IntelliJ插件
为了在IntelliJ IDEA中为我们的项目添加Appium Cucumber支持,我们需要安装“Gherkin”和“Cucumber for JAVA”插件。 你会下载,然后进入设置 - >插件,然后安装这些插件。
Cucumber 依赖
完成这一步之后,您需要将Cucumber Dependencies添加到您的pom.xml中。
Cucumber 依赖
XHTML
<!-- Cucumber Dependencies -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java8</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>1.2.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.12.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.masterthought/cucumber-reporting -->
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>3.14.0</version>
</dependency>
Cucumber 文件和文件夹
然后,我创建了一个Cucumber 包,在这个包下面,我创建了下面的包和文件:
?在功能包中,我们将保存所有功能文件。
?在步骤包中,我们将编写我们功能的步骤。 BaseSteps文件是所有步骤文件的通用/基本文件。
?在测试包中,我们将进行Cucumber 测试课程。
我将解释上述包和文件的细节。
Appium Cucumber TestNG测试自动化场景
我们的测试场景是isinolsun应用程序的成功登录。
在特征文件中,我们应该使用Gherkin语法编写我们的场景。
功能文件
Java
@Candidate
Feature: Candidate Successful Login Feature
Scenario: Login the app successfully as a candidate
Given I have skipped Splash and Tutorial pages and I am on the job selection pages
When I click ?s Ariyorum button
And I go to Profilim page
And I click Giris Yap button
And I fill "5910000010" as my telephone number
And I click Giri? Yap button
And I give permission for SMS
And I fill "35880" as a login code
And I click Tamam button
Then I should see my profile page
BaseSteps文件是所有步骤文件的基类。 在这个类中,我们为所有步骤文件声明了所有通用变量和函数。
BaseSteps
Java
public class BaseSteps {
protected SplashScreen splashScreen = null;
protected TutorialScreen tutorialScreen = null;
protected SelectionScreen selectionScreen = null;
protected CandidateMainScreen candidateMainScreen = null;
protected AndroidDriver<MobileElement> driver;
protected WebDriverWait wait;
//Screen Classes Initialization
protected void setupCucumber () {
System.out.println("Cucumber Base Test Before-login-test-cucumber");
//Get driver to use step files
driver = ThreadLocalDriver.getTLDriver();
wait = new WebDriverWait(driver, 10);
splashScreen = new SplashScreen(driver);
tutorialScreen = new TutorialScreen(driver);
selectionScreen = new SelectionScreen(driver);
candidateMainScreen = new CandidateMainScreen(driver);
}
protected void teardown(){
driver.quit();
}
}
我们需要在步骤文件中编写详细的操作。 在LoginSteps文件中,我编写了与我们的步骤相关的所有操作。
LoginSteps.java
Java
public class LoginSteps extends BaseSteps {
//Instantiations
ProfileLoginScreen profileLoginScreen;
LoginScreen loginScreen;
PassCodeScreen passCodeScreen;
MyProfileScreen myProfileScreen;
//Screen Classes Initialization
@Before
public void setupLoginSteps () {
System.out.println("Cucumber Before-login-test-cucumber");
setupCucumber();
profileLoginScreen = new ProfileLoginScreen(driver);
loginScreen = new LoginScreen(driver);
passCodeScreen = new PassCodeScreen(driver);
myProfileScreen = new MyProfileScreen(driver);
}
@Given("^I have skipped Splash and Tutorial pages and I am on the job selection pages$")
public void iHaveSkippedSplashAndTutorialPagesAndIAmOnTheJobSelectionPage() throws Throwable {
System.out.println("Cucumber Given");
// Write code here that turns the phrase above into concrete actions
splashScreen.skipSplashScreen();
tutorialScreen.skipTutorial();
}
@When("^I click ?s Ariyorum button$")
public void iClick?sAriyorumButton() throws Throwable {
// Write code here that turns the phrase above into concrete actions
selectionScreen.selectIsAriyorum();
candidateMainScreen.allowNotification();
}
@And("^I go to Profilim page$")
public void iGoToProfilimPage() throws Throwable {
// Write code here that turns the phrase above into concrete actions
candidateMainScreen.clickToProfile();
}
@And("^I click Giris Yap button$")
public void iClickGirisYapButton() throws Throwable {
// Write code here that turns the phrase above into concrete actions
profileLoginScreen.clickLogin();
}
@And("^I fill \"([^\"]*)\" as my telephone number$")
public void iFillAsMyTelephoneNumber(String arg0) throws Throwable {
// Write code here that turns the phrase above into concrete actions
loginScreen.enterPhoneNumber(arg0);
}
@And("^I click Giri? Yap button$")
public void iClickGiri?YapButton() throws Throwable {
// Write code here that turns the phrase above into concrete actions
loginScreen.clickLogin();
}
@And("^I give permission for SMS$")
public void iGivePermissionForSMS() throws Throwable {
// Write code here that turns the phrase above into concrete actions
passCodeScreen.allowNotification();
}
@And("^I fill \"([^\"]*)\" as a login code$")
public void iFillAsALoginCode(String arg0) throws Throwable {
passCodeScreen.sendPassCode();
}
@And("^I click Tamam button$")
public void iClickTamamButton() throws Throwable {
passCodeScreen.finishLogin();
}
@Then("^I should see my profile page$")
public void iShouldSeeMyProfilePage() throws Throwable {
myProfileScreen.checkWorkExperienceTitle();
}
@After
public synchronized void teardown () {
// teardown();
System.out.println("teardown'a girdi!");
}
}
现在是编写测试文件的时候了。 在下面的测试文件中,我们将运行所有功能并生成Cucumber报告。 如果你愿意,你也可以修改这个类来运行特定的Cucumber 功能。
RunCucumberFeatures
Java
@CucumberOptions(
monochrome = true,
tags = "@Candidate",
features = "src/test/java/tests/cucumber/features/LoginCandidate.feature",
glue = "tests.cucumber.steps" ,
format = {
"pretty",
"html:target/cucumber-reports/cucumber-pretty",
"json:target/cucumber-reports/CucumberTestReport.json",
"rerun:target/cucumber-reports/rerun.txt",
}
)
public class RunCucumberFeatures extends BaseTest {
private TestNGCucumberRunner testNGCucumberRunner;
@BeforeClass(alwaysRun = true)
public void setUpClass() {
System.out.println("Cucumber Test Class Before");
testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
}
@Test(groups = "cucumber", description = "Runs LoginCandiate Feature", dataProvider = "features")
public void feature(CucumberFeatureWrapper cucumberFeature) {
System.out.println("Cucumber Test Class Inside Test");
System.out.println(cucumberFeature.getCucumberFeature());
testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());
}
@DataProvider
public Object[][] features() {
System.out.println("Data Provider test Class");
return testNGCucumberRunner.provideFeatures();
}
@AfterClass(alwaysRun = true)
public void tearDownClass() throws Exception {
testNGCucumberRunner.finish();
}
}
我们应该改变我们的TestNG XML文件,如下所示进行并行执行。
的testng.xml
XHTML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="IsinOlsun-Android-Test-Suite" parallel="tests" thread-count="5">
<test name="Test-Sony-Xperia" parallel="classes" thread-count="5">
<parameter name="deviceName" value="Sony-Xperia" />
<parameter name="platformVersion" value="7.0" />
<classes>
<class name="tests.cucumber.tests.RunCucumberFeatures" />
</classes>
</test>
<test name="Test-LG-G4" parallel="classes" thread-count="5">
<parameter name="deviceName" value="LG-G4" />
<parameter name="platformVersion" value="6.0" />
<classes>
<class name="tests.cucumber.tests.RunCucumberFeatures" />
</classes>
</test>
</suite>
当你运行TestNG.xml文件时,你的移动测试并行运行。 在这个框架中,你可以使用Page Object Model和Cucumber和TestNG一起使用。
本文暂时没有评论,来添加一个吧(●'◡'●)