package com.appiumforios;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.HasTouchScreen;
import org.openqa.selenium.interactions.TouchScreen;
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.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class AndroidPrometheusTest {
private WebDriver driver;
@Before
public void setUp() throws Exception {
// set up appium
}
@After
public void tearDown() throws Exception {
driver.quit();
}
@Test
public void loginPrometheus(){
try
{
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "apps/ContactManager");
File app = new File(appDir, "Prometheus.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device","Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability(CapabilityType.VERSION, "4.4");
capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("app-package", "com.android");
capabilities.setCapability("app-activity", ".MainActivity");
// driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
/*
if(driver.findElement(By.xpath("//window[1]/scrollview[1]"))!=null)
{
((JavascriptExecutor)driver).executeScript("mobile: swipe", new HashMap<String, Double>() {{ put("startX", 292.0); put("startY", 270.0); put("endX", 24.0); put("endY", 288.0); put("duration", 0.8375); }});
}
WebElement el=driver.findElement(By.name("体验"));
driver.findElement(By.xpath("//window[1]")).click();
((JavascriptExecutor)driver).executeScript("mobile: swipe", new HashMap<String, Double>() {{ put("touchCount", 1.0); put("startX", 175.0); put("startY", 360.0); put("endX", 167.0); put("endY", 240.0); put("duration", 1.6475); }});
// 界面展示的内容,类似label ,用driver.findElements(By.tagName("text")); 或者 driver.findElement(By.name("规划"));
/*
List<WebElement> textFieldsList = driver.findElements(By.tagName("text"));
for(int i=0;i<textFieldsList.size();i++)
{
WebElement el=textFieldsList.get(i);
String s1=el.getText();
String s2=el.getAttribute("name");
String s3= el.getTagName();
}
*/
//界面展示的文本输入框
List<WebElement> textFieldsList = driver.findElements(By.className("android.widget.TextView"));
for(int i=0;i<textFieldsList.size();i++)
{
WebElement el=textFieldsList.get(i);
String s1=el.getText();
String s2=el.getAttribute("name");
String s3= el.getTagName();
}
}catch(Exception ex)
{
}
}
public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
private RemoteTouchScreen touch;
public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
super(remoteAddress, desiredCapabilities);
touch = new RemoteTouchScreen(getExecuteMethod());
}
@Override
public TouchScreen getTouch() {
return touch;
}
}
}