mirror of
https://github.com/appium/appium.git
synced 2026-01-15 14:49:55 -06:00
TestNG and Junit tests for Android.These tests are similar to the one available in the sample-code/examples/python
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package com.saucelabs.appium;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.openqa.selenium.*;
|
||||
import org.openqa.selenium.remote.CapabilityType;
|
||||
import org.openqa.selenium.remote.DesiredCapabilities;
|
||||
import org.openqa.selenium.remote.RemoteTouchScreen;
|
||||
import org.openqa.selenium.remote.RemoteWebDriver;
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
public class AndroidContactsTest {
|
||||
private WebDriver driver;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// set up appium
|
||||
File classpathRoot = new File(System.getProperty("user.dir"));
|
||||
File appDir = new File(classpathRoot, "../../../apps/ContactManager");
|
||||
File app = new File(appDir, "ContactManager.apk");
|
||||
DesiredCapabilities capabilities = new DesiredCapabilities();
|
||||
capabilities.setCapability("device","Android");
|
||||
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
|
||||
capabilities.setCapability(CapabilityType.VERSION, "4.2");
|
||||
capabilities.setCapability(CapabilityType.PLATFORM, "MAC");
|
||||
capabilities.setCapability("app", app.getAbsolutePath());
|
||||
capabilities.setCapability("app-package", "com.example.android.contactmanager");
|
||||
capabilities.setCapability("app-activity", ".ContactManager");
|
||||
driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
driver.quit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addContact(){
|
||||
WebElement el = driver.findElement(By.name("Add Contact"));
|
||||
el.click();
|
||||
List<WebElement> textFieldsList = driver.findElements(By.tagName("textfield"));
|
||||
textFieldsList.get(0).sendKeys("Some Name");
|
||||
textFieldsList.get(2).sendKeys("Some@example.com");
|
||||
driver.findElement(By.name("Save")).click();
|
||||
}
|
||||
|
||||
public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
|
||||
private RemoteTouchScreen touch;
|
||||
|
||||
public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
|
||||
super(remoteAddress, desiredCapabilities);
|
||||
touch = new RemoteTouchScreen(getExecuteMethod());
|
||||
}
|
||||
|
||||
public TouchScreen getTouch() {
|
||||
return touch;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.saucelabs.appium;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.openqa.selenium.*;
|
||||
import org.openqa.selenium.remote.CapabilityType;
|
||||
import org.openqa.selenium.remote.DesiredCapabilities;
|
||||
import org.openqa.selenium.remote.RemoteTouchScreen;
|
||||
import org.openqa.selenium.remote.RemoteWebDriver;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class AndroidTest {
|
||||
|
||||
private WebDriver driver;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
File classpathRoot = new File(System.getProperty("user.dir"));
|
||||
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin");
|
||||
File app = new File(appDir, "ApiDemos-debug.apk");
|
||||
DesiredCapabilities capabilities = new DesiredCapabilities();
|
||||
capabilities.setCapability("device","Android");
|
||||
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
|
||||
capabilities.setCapability(CapabilityType.VERSION, "4.2");
|
||||
capabilities.setCapability(CapabilityType.PLATFORM, "MAC");
|
||||
capabilities.setCapability("app", app.getAbsolutePath());
|
||||
capabilities.setCapability("app-package", "com.example.android.apis");
|
||||
capabilities.setCapability("app-activity", ".ApiDemos");
|
||||
driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
driver.quit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apiDemo(){
|
||||
WebElement el = driver.findElement(By.name("Animation"));
|
||||
assertEquals(el.getText(), "Animation");
|
||||
el = driver.findElement(By.tagName("text"));
|
||||
assertEquals(el.getText(), "API Demos");
|
||||
el = driver.findElement(By.name("App"));
|
||||
el.click();
|
||||
List<WebElement> els = driver.findElements(By.tagName("text"));
|
||||
assertEquals(els.get(2).getText(), "Activity");
|
||||
}
|
||||
|
||||
public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
|
||||
private RemoteTouchScreen touch;
|
||||
|
||||
public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
|
||||
super(remoteAddress, desiredCapabilities);
|
||||
touch = new RemoteTouchScreen(getExecuteMethod());
|
||||
}
|
||||
|
||||
public TouchScreen getTouch() {
|
||||
return touch;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.saucelabs.appium;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.openqa.selenium.*;
|
||||
import org.openqa.selenium.remote.DesiredCapabilities;
|
||||
import org.openqa.selenium.remote.RemoteTouchScreen;
|
||||
import org.openqa.selenium.remote.RemoteWebDriver;
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
|
||||
public class AndroidWebViewTest {
|
||||
private WebDriver driver;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// set up appium
|
||||
File classpathRoot = new File(System.getProperty("user.dir"));
|
||||
File appDir = new File(classpathRoot, "../../../apps/WebViewDemo/target");
|
||||
String file = getApkFile(appDir);
|
||||
File app = new File(appDir, file);
|
||||
DesiredCapabilities capabilities = new DesiredCapabilities();
|
||||
capabilities.setCapability("device","selendroid");
|
||||
capabilities.setCapability("app", app.getAbsolutePath());
|
||||
capabilities.setCapability("app-package", "io.selendroid.testapp");
|
||||
capabilities.setCapability("app-activity", ".HomeScreenActivity");
|
||||
driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
|
||||
}
|
||||
private String getApkFile(File appDir) {
|
||||
|
||||
for(String s : appDir.list())
|
||||
if(s.contains(".apk")){
|
||||
return s;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
driver.quit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void webView(){
|
||||
WebElement button = driver.findElement(By.id("buttonStartWebview"));
|
||||
button.click();
|
||||
driver.switchTo().window("WEBVIEW");
|
||||
WebElement inputField = driver.findElement(By.id("name_input"));
|
||||
inputField.sendKeys("Some name");
|
||||
inputField.submit();
|
||||
}
|
||||
|
||||
|
||||
public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
|
||||
private RemoteTouchScreen touch;
|
||||
|
||||
public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
|
||||
super(remoteAddress, desiredCapabilities);
|
||||
touch = new RemoteTouchScreen(getExecuteMethod());
|
||||
}
|
||||
|
||||
public TouchScreen getTouch() {
|
||||
return touch;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,32 +6,38 @@
|
||||
<artifactId>sauce_appium_testng</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>sauce_appium_testng</name>
|
||||
<description>Sample Appium tests using JUnit</description>
|
||||
<description>Sample Appium tests using TestNG</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.testng</groupId>
|
||||
<artifactId>testng</artifactId>
|
||||
<version>6.8</version>
|
||||
<scope>test</scope>
|
||||
<!--scope>test</scope-->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.seleniumhq.selenium</groupId>
|
||||
<artifactId>selenium-java</artifactId>
|
||||
<version>LATEST</version>
|
||||
<scope>test</scope>
|
||||
<!--scope>test</scope-->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.json-simple</groupId>
|
||||
<artifactId>json-simple</artifactId>
|
||||
<version>1.1</version>
|
||||
<scope>test</scope>
|
||||
<!--scope>test</scope-->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
<version>2.6</version>
|
||||
<scope>test</scope>
|
||||
<!--scope>test</scope-->
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.16</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.saucelabs.appium;
|
||||
|
||||
import org.openqa.selenium.*;
|
||||
import org.openqa.selenium.remote.CapabilityType;
|
||||
import org.openqa.selenium.remote.DesiredCapabilities;
|
||||
import org.openqa.selenium.remote.RemoteTouchScreen;
|
||||
import org.openqa.selenium.remote.RemoteWebDriver;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class AndroidContactsTest {
|
||||
private WebDriver driver;
|
||||
|
||||
@BeforeMethod
|
||||
public void setUp() throws Exception {
|
||||
// set up appium
|
||||
File classpathRoot = new File(System.getProperty("user.dir"));
|
||||
File appDir = new File(classpathRoot, "../../../apps/ContactManager");
|
||||
File app = new File(appDir, "ContactManager.apk");
|
||||
DesiredCapabilities capabilities = new DesiredCapabilities();
|
||||
capabilities.setCapability("device","Android");
|
||||
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
|
||||
capabilities.setCapability(CapabilityType.VERSION, "4.2");
|
||||
capabilities.setCapability(CapabilityType.PLATFORM, "MAC");
|
||||
capabilities.setCapability("app", app.getAbsolutePath());
|
||||
capabilities.setCapability("app-package", "com.example.android.contactmanager");
|
||||
capabilities.setCapability("app-activity", ".ContactManager");
|
||||
driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void tearDown() throws Exception {
|
||||
driver.quit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addContact(){
|
||||
WebElement el = driver.findElement(By.name("Add Contact"));
|
||||
el.click();
|
||||
List<WebElement> textFieldsList = driver.findElements(By.tagName("textfield"));
|
||||
textFieldsList.get(0).sendKeys("Some Name");
|
||||
textFieldsList.get(2).sendKeys("Some@example.com");
|
||||
driver.findElement(By.name("Save")).click();
|
||||
}
|
||||
|
||||
public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
|
||||
private RemoteTouchScreen touch;
|
||||
|
||||
public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
|
||||
super(remoteAddress, desiredCapabilities);
|
||||
touch = new RemoteTouchScreen(getExecuteMethod());
|
||||
}
|
||||
|
||||
public TouchScreen getTouch() {
|
||||
return touch;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.saucelabs.appium;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.openqa.selenium.*;
|
||||
import org.openqa.selenium.remote.CapabilityType;
|
||||
import org.openqa.selenium.remote.DesiredCapabilities;
|
||||
import org.openqa.selenium.remote.RemoteTouchScreen;
|
||||
import org.openqa.selenium.remote.RemoteWebDriver;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
public class AndroidTest {
|
||||
private WebDriver driver;
|
||||
|
||||
@BeforeMethod
|
||||
public void setUp() throws Exception {
|
||||
// set up appium
|
||||
File classpathRoot = new File(System.getProperty("user.dir"));
|
||||
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin");
|
||||
File app = new File(appDir, "ApiDemos-debug.apk");
|
||||
DesiredCapabilities capabilities = new DesiredCapabilities();
|
||||
capabilities.setCapability("device","Android");
|
||||
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
|
||||
capabilities.setCapability(CapabilityType.VERSION, "4.2");
|
||||
capabilities.setCapability(CapabilityType.PLATFORM, "MAC");
|
||||
capabilities.setCapability("app", app.getAbsolutePath());
|
||||
capabilities.setCapability("app-package", "com.example.android.apis");
|
||||
capabilities.setCapability("app-activity", ".ApiDemos");
|
||||
driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void tearDown() throws Exception {
|
||||
driver.quit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void apiDemo(){
|
||||
WebElement el = driver.findElement(By.name("Animation"));
|
||||
Assert.assertEquals(el.getText(),"Animation");
|
||||
el = driver.findElement(By.tagName("text"));
|
||||
Assert.assertEquals(el.getText(), "API Demos");
|
||||
el = driver.findElement(By.name("App"));
|
||||
el.click();
|
||||
List<WebElement> els = driver.findElements(By.tagName("text"));
|
||||
Assert.assertEquals(els.get(2).getText(),"Activity");
|
||||
}
|
||||
|
||||
public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
|
||||
private RemoteTouchScreen touch;
|
||||
|
||||
public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
|
||||
super(remoteAddress, desiredCapabilities);
|
||||
touch = new RemoteTouchScreen(getExecuteMethod());
|
||||
}
|
||||
|
||||
public TouchScreen getTouch() {
|
||||
return touch;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.saucelabs.appium;
|
||||
|
||||
import org.openqa.selenium.*;
|
||||
import org.openqa.selenium.remote.DesiredCapabilities;
|
||||
import org.openqa.selenium.remote.RemoteTouchScreen;
|
||||
import org.openqa.selenium.remote.RemoteWebDriver;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
|
||||
public class AndroidWebViewTest {
|
||||
|
||||
private WebDriver driver;
|
||||
|
||||
@BeforeMethod
|
||||
public void setUp() throws Exception {
|
||||
// set up appium
|
||||
File classpathRoot = new File(System.getProperty("user.dir"));
|
||||
File appDir = new File(classpathRoot, "../../../apps/WebViewDemo/target");
|
||||
String file = getApkFile(appDir);
|
||||
File app = new File(appDir, file);
|
||||
DesiredCapabilities capabilities = new DesiredCapabilities();
|
||||
capabilities.setCapability("device","selendroid");
|
||||
capabilities.setCapability("app", app.getAbsolutePath());
|
||||
capabilities.setCapability("app-package", "io.selendroid.testapp");
|
||||
capabilities.setCapability("app-activity", ".HomeScreenActivity");
|
||||
driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
|
||||
}
|
||||
|
||||
private String getApkFile(File appDir) {
|
||||
|
||||
for(String s : appDir.list())
|
||||
if(s.contains(".apk")){
|
||||
return s;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void tearDown() throws Exception {
|
||||
driver.quit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void webView(){
|
||||
WebElement button = driver.findElement(By.id("buttonStartWebview"));
|
||||
button.click();
|
||||
driver.switchTo().window("WEBVIEW");
|
||||
WebElement inputField = driver.findElement(By.id("name_input"));
|
||||
inputField.sendKeys("Some name");
|
||||
inputField.submit();
|
||||
}
|
||||
|
||||
public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
|
||||
private RemoteTouchScreen touch;
|
||||
|
||||
public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
|
||||
super(remoteAddress, desiredCapabilities);
|
||||
touch = new RemoteTouchScreen(getExecuteMethod());
|
||||
}
|
||||
|
||||
public TouchScreen getTouch() {
|
||||
return touch;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user