Skip to content

Commit

Permalink
Added 2.07 Parallel testing code
Browse files Browse the repository at this point in the history
  • Loading branch information
Lindsay Walker committed Dec 28, 2020
1 parent cf4301b commit f64cbd5
Show file tree
Hide file tree
Showing 18 changed files with 895 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Final/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<properties>
<testngXmlDir>src/test/resources/config</testngXmlDir>
<testngXmlFile>mobile_android_EMU_reporting_test.xml</testngXmlFile>
<testngXmlFile>mobile_android_EMU_parallel_test.xml</testngXmlFile>
<testng.version>6.14.3</testng.version>
<commons-logging.version>1.2</commons-logging.version>
<selenium.version>3.141.59</selenium.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
public class Mobile_Android_EMU_Parallel_Test {

private static final String APP = "Android.SauceLabs.Mobile.Sample.app.2.7.0.apk";
private static ThreadLocal<AndroidDriver> androidDriver = new ThreadLocal<AndroidDriver>(); //added

URL url;
private static ThreadLocal<AndroidDriver> androidDriver = new ThreadLocal<AndroidDriver>(); //added
// private AndroidDriver driver; //deleted

String usernameID = "test-Username";
Expand Down Expand Up @@ -57,8 +56,7 @@ public void setUp (Method method) throws Exception { // add later
capabilities.setCapability("app", "storage:filename=" + APP);
capabilities.setCapability("name", methodName);


androidDriver.set(new AndroidDriver(url, capabilities));// added
androidDriver.set(new AndroidDriver(url, capabilities));// updated
}

@AfterMethod
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Android Mobile Demo Suite" parallel="methods"
thread-count="10" verbose="0">
<suite name="Android Mobile Demo Suite" parallel="methods" thread-count="10" verbose="1">

<test name="Simple Emulator Test Android" parallel="methods" enabled="true">
<classes>
Expand Down
105 changes: 105 additions & 0 deletions Mod2/2.07/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>SauceAppiumSample</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<testngXmlDir>src/test/resources/config</testngXmlDir>
<testngXmlFile>mobile_android_EMU_parallel_test.xml</testngXmlFile>
<testng.version>6.14.3</testng.version>
<commons-logging.version>1.2</commons-logging.version>
<selenium.version>3.141.59</selenium.version>
<maven-failsafe-plugin.version>2.19.1</maven-failsafe-plugin.version>
<sauce.version>1.0.44</sauce.version>
<appium.version>7.2.0</appium.version>
</properties>

<dependencies>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
</dependency>

<!-- Selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>${selenium.version}</version>
</dependency>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>

<!-- Apache Commons Logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${commons-logging.version}</version>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.10.0</version>
</dependency>

<dependency>
<groupId>com.saucelabs</groupId>
<artifactId>saucerest</artifactId>
<version>${sauce.version}</version>
</dependency>

<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>${appium.version}</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${testngXmlDir}/${testngXmlFile}</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
104 changes: 104 additions & 0 deletions Mod2/2.07/src/test/java/tests/BasicTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package tests;

import io.appium.java_client.MobileBy;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.net.URL;


public class BasicTest {

private static final String APP = "/Users/lindsaywalker/Documents/Example_Tests/Android.SauceLabs.Mobile.Sample.app.2.7.0.apk";
private static final String APPIUM = "http://localhost:4723/wd/hub";

private AndroidDriver driver;

String usernameID = "test-Username";
String passwordID = "test-Password";
String submitButtonID = "test-LOGIN";
By ProductTitle = By.xpath("//android.widget.TextView[@text='PRODUCTS']");

@BeforeMethod
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Android Emulator"); //This will change to type of device e.g. Pixel 4 on Saucelabs
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion","9.0" ); //add platformVersion
capabilities.setCapability("automationName", "UiAutomator2");
capabilities.setCapability("appWaitActivity", "com.swaglabsmobileapp.MainActivity");
capabilities.setCapability("app", APP);


driver = new AndroidDriver(new URL(APPIUM), capabilities);

}

@AfterMethod
public void tearDown() {
if (driver != null) {
driver.quit();
}

}

@Test
public void loginToSwagLabsTestValid() {
System.out.println("Sauce - Start loginToSwagLabsTestValid test");

login("standard_user", "secret_sauce");

// Verification
Assert.assertTrue(isOnProductsPage());
}

@Test
public void loginTestValidProblem() {
System.out.println("Sauce - Start loginTestValidProblem test");

login("problem_user", "secret_sauce");

// Verification - we on Product page
Assert.assertTrue(isOnProductsPage());
}

public void login(String user, String pass){

WebDriverWait wait = new WebDriverWait(driver, 5);
final WebElement usernameEdit = wait.until(ExpectedConditions.visibilityOfElementLocated(new MobileBy.ByAccessibilityId(usernameID)));

usernameEdit.click();
usernameEdit.sendKeys(user);

WebElement passwordEdit = driver.findElementByAccessibilityId(passwordID);
passwordEdit.click();
passwordEdit.sendKeys(pass);

WebElement submitButton = driver.findElementByAccessibilityId(submitButtonID);
submitButton.click();
}

public boolean isOnProductsPage() {
//Create an instance of a Appium explicit wait so that we can dynamically wait for an element
WebDriverWait wait = new WebDriverWait(driver, 5);

//wait for the product field to be visible and store that element into a variable
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(ProductTitle));
} catch (TimeoutException e){
System.out.println("*** Timed out waiting for product page to load.");
return false;
}
return true;
}
}

6 changes: 6 additions & 0 deletions Mod2/2.07/src/test/java/tests/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tests;

public class Config {
public static final String region = System.getProperty("region", "us");

}
96 changes: 96 additions & 0 deletions Mod2/2.07/src/test/java/tests/Mobile_Android_Browser_Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package tests;

import io.appium.java_client.MobileBy;// removed
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException; // removed
import org.openqa.selenium.WebElement; // removed
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions; // removed
import org.openqa.selenium.support.ui.WebDriverWait; // removed
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.lang.reflect.Method;
import java.net.URL;

import static tests.Config.region;

public class Mobile_Android_Browser_Test {
// private static final String APP = "/Users/lindsaywalker/Documents/Example_Tests/Android.SauceLabs.Mobile.Sample.app.2.7.0.apk";
// private static final String APPIUM = "http://localhost:4723/wd/hub"; // See the new URL declared according to region.
String appUrl = "https://www.saucedemo.com/"; //added
private AndroidDriver driver;
// String usernameID = "test-Username";
// String passwordID = "test-Password";
// String submitButtonID = "test-LOGIN";
// By ProductTitle = By.xpath("//android.widget.TextView[@text='PRODUCTS']");
By usernameInput = By.id("user-name");
By passwordInput = By.id("password");
By submitButton = By.className("btn_action");
By productTitle = By.className("product_label");
//all 4 vars changed from app test

@BeforeMethod
public void setUp () throws Exception {
System.out.println("Sauce Android Mobile Browser EMU - BeforeMethod hook"); //added
// URL url; //added
String username = System.getenv("SAUCE_USERNAME");
String accesskey = System.getenv("SAUCE_ACCESS_KEY");
String sauceUrl;
if (region.equalsIgnoreCase("eu")) {
sauceUrl = "@ondemand.eu-central-1.saucelabs.com:443";
} else {
sauceUrl = "@ondemand.us-west-1.saucelabs.com:443";
}
String SAUCE_REMOTE_URL = "https://" + username + ":" + accesskey + sauceUrl + "/wd/hub";
URL url = new URL(SAUCE_REMOTE_URL);

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Android GoogleAPI Emulator"); //changed to GoogleAPI Emulator
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion","9.0" );
capabilities.setCapability("automationName", "UiAutomator2");
// capabilities.setCapability("appWaitActivity", "com.swaglabsmobileapp.MainActivity"); //deleted
capabilities.setCapability("browserName", "Chrome");// added
// driver = new AndroidDriver(new URL(APPIUM), capabilities); //removed
driver = new AndroidDriver(url, capabilities); //added

}

@AfterMethod
public void teardown() {
if (driver != null) {
driver.quit();
}

}

@Test
public void loginToSwagLabsTestValid() {
System.out.println("Sauce - Start loginToSwagLabsTestValid test");
login("standard_user", "secret_sauce");

// Verification
Assert.assertTrue(isOnProductsPage());
}

public void login(String user, String pass){
driver.get(appUrl);
driver.findElement(usernameInput).sendKeys(user);
driver.findElement(passwordInput).sendKeys(pass);

driver.findElement(submitButton).click();

}

public boolean isOnProductsPage() {

return driver.findElement(productTitle).isDisplayed();
}
}


Loading

0 comments on commit f64cbd5

Please sign in to comment.