diff --git a/sample-code/examples/java/junit/README.md b/sample-code/examples/java/junit/README.md index 0732e4d1a..2a10e5de4 100644 --- a/sample-code/examples/java/junit/README.md +++ b/sample-code/examples/java/junit/README.md @@ -7,7 +7,7 @@ In order to run the tests, you will need to install [Apache Maven](http://maven. You will then need to start appium, eg: - grunt appium + appium To compile and run all tests, run: @@ -15,4 +15,4 @@ To compile and run all tests, run: To run a single test, run: - mvn -Dtest=com.saucelabs.appium.SimpleTest test \ No newline at end of file + mvn -Dtest=com.saucelabs.appium.SimpleTest test diff --git a/sample-code/examples/java/junit/pom.xml b/sample-code/examples/java/junit/pom.xml index 51fac79e8..65bf4e7ca 100644 --- a/sample-code/examples/java/junit/pom.xml +++ b/sample-code/examples/java/junit/pom.xml @@ -20,6 +20,11 @@ LATEST test + + io.appium + java-client + 1.0.2 + com.googlecode.json-simple json-simple diff --git a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/AndroidContactsTest.java b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/AndroidContactsTest.java index 2a8152917..40fcc7007 100644 --- a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/AndroidContactsTest.java +++ b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/AndroidContactsTest.java @@ -1,21 +1,20 @@ package com.saucelabs.appium; +import io.appium.java_client.AppiumDriver; 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.By; +import org.openqa.selenium.WebElement; 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; + private AppiumDriver driver; @Before public void setUp() throws Exception { @@ -26,12 +25,11 @@ public class AndroidContactsTest { 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(CapabilityType.VERSION, "4.4"); 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); + driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); } @After @@ -43,22 +41,11 @@ public class AndroidContactsTest { public void addContact(){ WebElement el = driver.findElement(By.name("Add Contact")); el.click(); - List textFieldsList = driver.findElements(By.tagName("textfield")); + List textFieldsList = driver.findElementsByClassName("android.widget.EditText"); textFieldsList.get(0).sendKeys("Some Name"); textFieldsList.get(2).sendKeys("Some@example.com"); - driver.findElement(By.name("Save")).click(); + driver.swipe(100, 500, 100, 100, 2); + driver.findElementByName("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; - } - } } diff --git a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/AndroidTest.java b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/AndroidTest.java index 96e0f9890..13708158a 100644 --- a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/AndroidTest.java +++ b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/AndroidTest.java @@ -1,15 +1,13 @@ package com.saucelabs.appium; +import io.appium.java_client.AppiumDriver; 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.By; +import org.openqa.selenium.WebElement; 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; @@ -19,7 +17,7 @@ import static org.junit.Assert.assertEquals; public class AndroidTest { - private WebDriver driver; + private AppiumDriver driver; @Before public void setUp() throws Exception { @@ -29,12 +27,11 @@ public class AndroidTest { 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(CapabilityType.VERSION, "4.4"); 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); + driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); } @After @@ -46,27 +43,14 @@ public class AndroidTest { public void apiDemo(){ WebElement el = driver.findElement(By.name("Animation")); assertEquals("Animation", el.getText()); - el = driver.findElement(By.tagName("text")); + el = driver.findElementByClassName("android.widget.TextView"); assertEquals("API Demos", el.getText()); el = driver.findElement(By.name("App")); el.click(); - List els = driver.findElements(By.tagName("text")); + List els = driver.findElementsByClassName("android.widget.TextView"); assertEquals("Activity", els.get(2).getText()); } - 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; - } - } - } diff --git a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/AndroidWebViewTest.java b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/AndroidWebViewTest.java index 6abb9ebef..ba229db00 100644 --- a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/AndroidWebViewTest.java +++ b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/AndroidWebViewTest.java @@ -1,19 +1,20 @@ package com.saucelabs.appium; +import io.appium.java_client.AppiumDriver; 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.By; +import org.openqa.selenium.WebElement; +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.Set; public class AndroidWebViewTest { - private WebDriver driver; + private AppiumDriver driver; @Before public void setUp() throws Exception { @@ -21,11 +22,12 @@ public class AndroidWebViewTest { File classpathRoot = new File(System.getProperty("user.dir")); File app = new File(classpathRoot, "../../../apps/selendroid-test-app.apk"); DesiredCapabilities capabilities = new DesiredCapabilities(); - capabilities.setCapability("device","selendroid"); + capabilities.setCapability("automationName","selendroid"); + capabilities.setCapability(CapabilityType.PLATFORM, "android"); 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); + driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); } @After public void tearDown() throws Exception { @@ -33,27 +35,20 @@ public class AndroidWebViewTest { } @Test - public void webView(){ + public void webView() throws InterruptedException { WebElement button = driver.findElement(By.id("buttonStartWebview")); button.click(); - driver.switchTo().window("WEBVIEW"); + Thread.sleep(6000); + Set contextNames = driver.getContextHandles(); + for (String contextName : contextNames) { + System.out.println(contextName); + if (contextName.contains("WEBVIEW")){ + driver.context(contextName); + } + } 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; - } - } - } diff --git a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/MobileFindJavaTest.java b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/MobileFindJavaTest.java index 81fb6a9cd..fa88aec3b 100644 --- a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/MobileFindJavaTest.java +++ b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/MobileFindJavaTest.java @@ -1,49 +1,35 @@ package com.saucelabs.appium; -import java.net.URL; - -import org.apache.http.HttpEntity; +import com.google.gson.JsonParser; +import io.appium.java_client.AppiumDriver; import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; -import org.apache.http.util.EntityUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; -import org.openqa.selenium.remote.RemoteWebDriver; -import org.openqa.selenium.remote.RemoteWebElement; -import com.google.gson.JsonParser; +import java.net.URL; public class MobileFindJavaTest { - private RemoteWebDriver driver; + private AppiumDriver driver; private static final String url = "http://127.0.0.1:4723/wd/hub"; private static final HttpClient client = HttpClients.createDefault(); private static final JsonParser parser = new JsonParser(); @Test public void apiDemo() throws Exception { - final WebElement about_phone = scroll_to("about phone"); + final String about_phone = scroll_to("about phone"); if (about_phone != null) { - System.out.println("scrolled to: " + about_phone.getText()); + System.out.println("scrolled to: aboutPhone"); + System.out.println("returned: " + about_phone); } scroll_to("bluetooth"); } - /** Create a new remote web element. **/ - public RemoteWebElement newElement(final String elementId) { - final RemoteWebElement element = new RemoteWebElement(); - element.setParent(driver); - element.setId(elementId); - element.setFileDetector(driver.getFileDetector()); - return element; - } // @formatter:off /* @@ -65,52 +51,24 @@ public class MobileFindJavaTest { end */ // @formatter:on - public WebElement scroll_to(String text) { - RemoteWebElement element = null; - try { - text = text.replaceAll("\"", "\\\""); // quotes must be escaped. - final String jsonString = "{\"script\":\"mobile: find\",\"args\":[[\"scroll\",[[3,\"" - + text + "\"]],[[7,\"" + text + "\"]]]]}"; - final String id = driver.getSessionId().toString(); - final String executeURL = url + "/session/" + id + "/execute"; + public String scroll_to(String text) { - final HttpPost post = new HttpPost(executeURL); - post.setEntity(new StringEntity(jsonString, "UTF8")); - post.setHeader("Content-type", "application/json"); + text = text.replaceAll("\"", "\\\""); // quotes must be escaped. + final String[] jsonString = {"\"scroll\"","[[3,\"" + text + "\"]]","[[7,\"" + text + "\"]]"}; - final HttpEntity responseEntity = client.execute(post).getEntity(); - if (responseEntity != null) { - try { - final String responseString = EntityUtils.toString(responseEntity); - // {"status":0,"value":{"ELEMENT":"1"},"sessionId":"8e982755-980f-4036-b3d1-c0e14e890273"} - final String elementId = parser.parse(responseString) - .getAsJsonObject().get("value").getAsJsonObject().get("ELEMENT") - .getAsString(); + return driver.complexFind(jsonString); - element = newElement(elementId); - } catch (final Exception e) { - e.printStackTrace(); - } finally { - EntityUtils.consume(responseEntity); - } - } - } catch (final Exception e) { - e.printStackTrace(); - } - return element; } @Before public void setUp() throws Exception { final 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-package", "com.android.settings"); - capabilities.setCapability("app-activity", ".Settings"); - driver = new RemoteWebDriver(new URL(url), capabilities); + capabilities.setCapability("device", "android"); + capabilities.setCapability(CapabilityType.PLATFORM, "android"); + capabilities.setCapability("appPackage", "com.android.settings"); + capabilities.setCapability("appActivity", ".Settings"); + driver = new AppiumDriver(new URL(url), capabilities); } @After diff --git a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SafariTest.java b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SafariTest.java index 80f40f4d5..d8ba952ba 100644 --- a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SafariTest.java +++ b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SafariTest.java @@ -10,7 +10,6 @@ import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import java.net.URL; -import java.util.concurrent.TimeUnit; import static org.junit.Assert.*; @@ -35,11 +34,12 @@ public class SafariTest { public void setUp() throws Exception { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("device", "iPhone Simulator"); - capabilities.setCapability("version", "6.1"); - capabilities.setCapability("app", "safari"); + capabilities.setCapability("platformName", "iOS"); + capabilities.setCapability("version", "7.1"); + capabilities.setCapability("browserName", "safari"); driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); - driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); + //driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } /** @@ -50,6 +50,7 @@ public class SafariTest { @Test public void runTest() throws Exception { driver.get("http://saucelabs.com/test/guinea-pig"); + Thread.sleep(1000); WebElement idElement = driver.findElement(By.id("i_am_an_id")); assertNotNull(idElement); assertEquals("I am a div", idElement.getText()); @@ -59,10 +60,12 @@ public class SafariTest { WebElement submitElement = driver.findElement(By.id("submit")); assertNotNull(submitElement); submitElement.click(); + Thread.sleep(7000); WebElement yourCommentsElement = driver.findElement(By.id("your_comments")); assertNotNull(yourCommentsElement); assertTrue(driver.findElement(By.id("your_comments")).getText().contains("This is an awesome comment")); + System.out.println(driver.getCurrentUrl()); } /** diff --git a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SauceTest.java b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SauceTest.java index 97fb13424..96a5a7398 100644 --- a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SauceTest.java +++ b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SauceTest.java @@ -3,12 +3,11 @@ package com.saucelabs.appium; import com.saucelabs.common.SauceOnDemandAuthentication; import com.saucelabs.common.SauceOnDemandSessionIdProvider; import com.saucelabs.junit.SauceOnDemandTestWatcher; +import io.appium.java_client.AppiumDriver; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.openqa.selenium.By; -import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; @@ -38,7 +37,7 @@ import static org.junit.Assert.assertEquals; */ public class SauceTest implements SauceOnDemandSessionIdProvider { - private WebDriver driver; + private AppiumDriver driver; private List values; @@ -76,7 +75,7 @@ public class SauceTest implements SauceOnDemandSessionIdProvider { capabilities.setCapability(CapabilityType.PLATFORM, "Mac 10.8"); capabilities.setCapability("app", "http://appium.s3.amazonaws.com/TestApp6.0.app.zip"); - driver = new RemoteWebDriver(new URL(MessageFormat.format("http://{0}:{1}@ondemand.saucelabs.com:80/wd/hub", sauceUserName, sauceAccessKey)), + driver = new AppiumDriver(new URL(MessageFormat.format("http://{0}:{1}@ondemand.saucelabs.com:80/wd/hub", sauceUserName, sauceAccessKey)), capabilities); this.sessionId = ((RemoteWebDriver) driver).getSessionId().toString(); values = new ArrayList(); @@ -89,7 +88,7 @@ public class SauceTest implements SauceOnDemandSessionIdProvider { private void populate() { //populate text fields with two random number - List elems = driver.findElements(By.tagName("textField")); + List elems = driver.findElementsByClassName("UIATextField"); Random random = new Random(); for (WebElement elem : elems) { int rndNum = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; @@ -104,10 +103,10 @@ public class SauceTest implements SauceOnDemandSessionIdProvider { // populate text fields with values populate(); // trigger computation by using the button - WebElement button = driver.findElement(By.tagName("button")); + WebElement button = driver.findElementByClassName("UIAButton"); button.click(); // is sum equal ? - WebElement texts = driver.findElement(By.tagName("staticText")); + WebElement texts = driver.findElementByClassName("UIAStaticText"); assertEquals(String.valueOf(values.get(0) + values.get(1)), texts.getText()); } diff --git a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SimpleTest.java b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SimpleTest.java index 492f5e7db..9ae41bf47 100644 --- a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SimpleTest.java +++ b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SimpleTest.java @@ -1,34 +1,21 @@ package com.saucelabs.appium; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; - -import org.apache.http.util.EntityUtils; +import io.appium.java_client.AppiumDriver; +import io.appium.java_client.MobileBy; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; - -import org.openqa.selenium.Dimension; -import org.openqa.selenium.Alert; -import org.openqa.selenium.By; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.WebElement; -import org.openqa.selenium.Point; -import org.openqa.selenium.interactions.HasTouchScreen; -import org.openqa.selenium.Capabilities; -import org.openqa.selenium.interactions.TouchScreen; +import org.apache.http.util.EntityUtils; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +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.RemoteWebDriver; -import org.openqa.selenium.remote.RemoteTouchScreen; -import org.openqa.selenium.interactions.Actions; -import org.openqa.selenium.interactions.touch.TouchActions; import java.io.File; import java.net.URL; @@ -47,7 +34,7 @@ import static org.junit.Assert.assertTrue; */ public class SimpleTest { - private WebDriver driver; + private AppiumDriver driver; private List values; @@ -61,11 +48,11 @@ public class SimpleTest { File app = new File(appDir, "TestApp.app"); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); - capabilities.setCapability(CapabilityType.VERSION, "6.0"); + capabilities.setCapability(CapabilityType.VERSION, "7.1"); capabilities.setCapability(CapabilityType.PLATFORM, "Mac"); capabilities.setCapability("device", "iPhone Simulator"); capabilities.setCapability("app", app.getAbsolutePath()); - driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); + driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); values = new ArrayList(); } @@ -76,7 +63,7 @@ public class SimpleTest { private void populate() { //populate text fields with two random number - List elems = driver.findElements(By.tagName("textField")); + List elems = driver.findElements(By.className("UIATextField")); Random random = new Random(); for (WebElement elem : elems) { int rndNum = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; @@ -85,52 +72,53 @@ public class SimpleTest { } } + private Point getCenter(WebElement element) { + + Point upperLeft = element.getLocation(); + Dimension dimensions = element.getSize(); + return new Point(upperLeft.getX() + dimensions.getWidth()/2, upperLeft.getY() + dimensions.getHeight()/2); + } + @Test public void testUIComputation() throws Exception { // populate text fields with values populate(); // trigger computation by using the button - WebElement button = driver.findElement(By.tagName("button")); + WebElement button = driver.findElement(By.className("UIAButton")); button.click(); // is sum equal ? - WebElement texts = driver.findElement(By.tagName("staticText")); + WebElement texts = driver.findElement(By.className("UIAStaticText")); assertEquals(String.valueOf(values.get(0) + values.get(1)), texts.getText()); } @Test public void testActive() throws Exception { - WebElement text = driver.findElement(By.xpath("//textfield[1]")); + WebElement text = driver.findElement(By.xpath("//UIATextField[1]")); assertTrue(text.isDisplayed()); - WebElement button = driver.findElement(By.xpath("//button[1]")); + WebElement button = driver.findElement(By.xpath("//UIAButton[1]")); assertTrue(button.isDisplayed()); } @Test public void testBasicAlert() throws Exception { - driver.findElement(By.xpath("//button[2]")).click(); + driver.findElement(By.xpath("//UIAButton[2]")).click(); Alert alert = driver.switchTo().alert(); //check if title of alert is correct - assertEquals("Cool title", alert.getText()); + assertEquals("Cool title this alert is so cool.", alert.getText()); alert.accept(); } - @Test - public void testBasicTagName() throws Exception { - WebElement text = driver.findElement(By.xpath("//textfield[1]")); - assertEquals("UIATextField", text.getTagName()); - } - @Test public void testBasicButton() throws Exception { - WebElement button = driver.findElement(By.xpath("//button[1]")); + WebElement button = driver.findElement(By.xpath("//UIAButton[1]")); assertEquals("ComputeSumButton", button.getText()); } @Test public void testClear() throws Exception { - WebElement text = driver.findElement(By.xpath("//textfield[1]")); + WebElement text = driver.findElement(By.xpath("//UIATextField[1]")); text.sendKeys("12"); text.clear(); @@ -139,114 +127,49 @@ public class SimpleTest { @Test public void testHideKeyboard() throws Exception { - driver.findElement(By.xpath("//textfield[1]")).sendKeys("12"); + driver.findElement(By.xpath("//UIATextField[1]")).sendKeys("12"); - WebElement button = driver.findElement(By.name("Done")); + WebElement button = driver.findElement(MobileBy.AccessibilityId("Done")); assertTrue(button.isDisplayed()); button.click(); } @Test - public void testFindElementByTagName() throws Exception { + public void testFindElementByClassName() throws Exception { Random random = new Random(); - WebElement text = driver.findElement(By.tagName("textField")); + WebElement text = driver.findElementByClassName("UIATextField"); int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; text.sendKeys(String.valueOf(number)); - driver.findElement(By.tagName("button")).click(); + driver.findElementByClassName("UIAButton").click(); // is sum equal ? - WebElement sumLabel = driver.findElement(By.tagName("staticText")); + WebElement sumLabel = driver.findElementByClassName("UIAStaticText"); assertEquals(String.valueOf(number), sumLabel.getText()); } @Test - public void testFindElementsByTagName() throws Exception { - Random random = new Random(); + public void testFindElementsByClassName() throws Exception { + Random random = new Random(); - WebElement text = driver.findElements(By.tagName("textField")).get(1); + WebElement text = driver.findElementsByClassName("UIATextField").get(1); + int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; + text.sendKeys(String.valueOf(number)); - int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; - text.sendKeys(String.valueOf(number)); + driver.findElementByClassName("UIAButton").click(); - driver.findElements(By.tagName("button")).get(0).click(); - - // is sum equal ? - WebElement texts = driver.findElements(By.tagName("staticText")).get(0); - assertEquals(String.valueOf(number), texts.getText()); - } - - @Test - public void testFindElementByName() throws Exception { - Random random = new Random(); - - WebElement text = driver.findElement(By.name("TextField1")); - - int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; - text.sendKeys(String.valueOf(number)); - - // is sum equal ? - WebElement sumLabel = driver.findElement(By.name("SumLabel")); - driver.findElement(By.name("ComputeSumButton")).click(); - - assertEquals(String.valueOf(number), sumLabel.getText()); - } - - @Test - public void testFindElementsByName() throws Exception { - Random random = new Random(); - - WebElement text = driver.findElements(By.name("TextField1")).get(0); - - int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; - text.sendKeys(String.valueOf(number)); - - // is sum equal ? - WebElement sumLabel = driver.findElements(By.name("SumLabel")).get(0); - driver.findElements(By.name("ComputeSumButton")).get(0).click(); - - assertEquals(String.valueOf(number), sumLabel.getText()); - } - - @Test - public void testFindElementByXpath() throws Exception { - Random random = new Random(); - - WebElement text = driver.findElement(By.xpath("//textfield[1]")); - - int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; - text.sendKeys(String.valueOf(number)); - - // is sum equal ? - driver.findElement(By.xpath("//button[1]")).click(); - - WebElement sumLabel = driver.findElement(By.xpath("//text[1]")); - assertEquals(String.valueOf(number), sumLabel.getText()); - } - - @Test - public void testFindElementsByXpath() throws Exception { - Random random = new Random(); - - WebElement text = driver.findElements(By.xpath("//textfield")).get(1); - - int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; - text.sendKeys(String.valueOf(number)); - - // is sum equal ? - driver.findElements(By.xpath("//button")).get(0).click(); - - WebElement sumLabel = driver.findElements(By.xpath("//text")).get(0); - assertEquals(String.valueOf(number), sumLabel.getText()); + // is sum equal ? + WebElement sumLabel = driver.findElementsByClassName("UIAStaticText").get(0); + assertEquals(String.valueOf(number), sumLabel.getText()); } @Test public void testAttribute() throws Exception { Random random = new Random(); - WebElement text = driver.findElement(By.xpath("//textfield[1]")); + WebElement text = driver.findElement(By.xpath("//UIATextField[1]")); int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; text.sendKeys(String.valueOf(number)); @@ -259,16 +182,17 @@ public class SimpleTest { @Test public void testSlider() throws Exception { //get the slider - WebElement slider = driver.findElement(By.xpath("//slider[1]")); - assertEquals("50 %", slider.getAttribute("value")); - TouchActions drag = new TouchActions(driver).flick(slider, new Integer(-1), 0, 0); - drag.perform(); - assertEquals("0 %", slider.getAttribute("value")); + WebElement slider = driver.findElement(By.xpath("//UIASlider[1]")); + assertEquals("50%", slider.getAttribute("value")); + Point sliderLocation = getCenter(slider); + driver.swipe(sliderLocation.getX(), sliderLocation.getY(), sliderLocation.getX()-100, sliderLocation.getY(), 1); + + assertEquals("0%", slider.getAttribute("value")); } @Test public void testLocation() throws Exception { - WebElement button = driver.findElement(By.xpath("//button[1]")); + WebElement button = driver.findElement(By.xpath("//UIAButton[1]")); Point location = button.getLocation(); @@ -284,28 +208,15 @@ public class SimpleTest { HttpEntity entity = response.getEntity(); JSONObject jsonObject = (JSONObject) new JSONParser().parse(EntityUtils.toString(entity)); - String sessionId = ((RemoteWebDriver) driver).getSessionId().toString(); + String sessionId = driver.getSessionId().toString(); assertEquals(jsonObject.get("sessionId"), sessionId); } @Test public void testSize() { - Dimension text1 = driver.findElement(By.xpath("//textfield[1]")).getSize(); - Dimension text2 = driver.findElement(By.xpath("//textfield[2]")).getSize(); + Dimension text1 = driver.findElement(By.xpath("//UIATextField[1]")).getSize(); + Dimension text2 = driver.findElement(By.xpath("//UIATextField[2]")).getSize(); assertEquals(text1.getWidth(), text2.getWidth()); assertEquals(text1.getHeight(), text2.getHeight()); } - - 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; - } - } } diff --git a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/UICatalogTest.java b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/UICatalogTest.java index 367e6eecc..98254b520 100644 --- a/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/UICatalogTest.java +++ b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/UICatalogTest.java @@ -1,5 +1,7 @@ package com.saucelabs.appium; +import io.appium.java_client.AppiumDriver; +import io.appium.java_client.MobileElement; import org.apache.commons.lang.RandomStringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; @@ -11,14 +13,13 @@ import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.openqa.selenium.*; import org.openqa.selenium.interactions.Actions; -import org.openqa.selenium.interactions.HasTouchScreen; -import org.openqa.selenium.interactions.TouchScreen; -import org.openqa.selenium.interactions.touch.TouchActions; -import org.openqa.selenium.remote.*; +import org.openqa.selenium.remote.Augmenter; +import org.openqa.selenium.remote.CapabilityType; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.RemoteWebElement; import java.io.File; import java.net.URL; @@ -34,7 +35,7 @@ import static org.junit.Assert.*; */ public class UICatalogTest { - private WebDriver driver; + private AppiumDriver driver; private WebElement row; @@ -46,11 +47,11 @@ public class UICatalogTest { File app = new File(appDir, "UICatalog.app"); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.BROWSER_NAME, ""); - capabilities.setCapability(CapabilityType.VERSION, "6.1"); + capabilities.setCapability(CapabilityType.VERSION, "7.1"); capabilities.setCapability(CapabilityType.PLATFORM, "Mac"); capabilities.setCapability("device", "iPhone Simulator"); capabilities.setCapability("app", app.getAbsolutePath()); - driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); + driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); } @After @@ -60,32 +61,39 @@ public class UICatalogTest { private void openMenuPosition(int index) { //populate text fields with two random number - WebElement table = driver.findElement(By.tagName("tableView")); - row = table.findElements(By.tagName("tableCell")).get(index); + MobileElement table = new MobileElement((RemoteWebElement)driver.findElementByClassName("UIATableView"), driver); + row = table.findElementsByClassName("UIATableCell").get(index); row.click(); } + private Point getCenter(WebElement element) { + + Point upperLeft = element.getLocation(); + Dimension dimensions = element.getSize(); + return new Point(upperLeft.getX() + dimensions.getWidth()/2, upperLeft.getY() + dimensions.getHeight()/2); + } + @Test public void testFindElement() throws Exception { //first view in UICatalog is a table - WebElement table = driver.findElement(By.tagName("tableView")); + MobileElement table = new MobileElement((RemoteWebElement)driver.findElementByClassName("UIATableView"), driver); assertNotNull(table); //is number of cells/rows inside table correct - List rows = table.findElements(By.tagName("tableCell")); + List rows = table.findElementsByClassName("UIATableCell"); assertEquals(12, rows.size()); //is first one about buttons assertEquals("Buttons, Various uses of UIButton", rows.get(0).getAttribute("name")); //navigationBar is not inside table WebElement nav_bar = null; try { - nav_bar = table.findElement(By.tagName("navigationBar")); + nav_bar = table.findElementByClassName("UIANavigationBar"); } catch (NoSuchElementException e) { //expected } assertNull(nav_bar); //there is nav bar inside the app driver.getPageSource(); - nav_bar = driver.findElement(By.tagName("navigationBar")); + nav_bar = driver.findElementByClassName("UIANavigationBar"); assertNotNull(nav_bar); } @@ -93,13 +101,12 @@ public class UICatalogTest { @Test public void test_location() { //get third row location - row = driver.findElements(By.tagName("tableCell")).get(2); + row = driver.findElementsByClassName("UIATableCell").get(2); assertEquals(0, row.getLocation().getX()); assertEquals(152, row.getLocation().getY()); } @Test - @Ignore("Currently failing due to IllegalArgumentException: Superclass has no null constructors but no arguments were given") public void testScreenshot() { //make screenshot and get is as base64 WebDriver augmentedDriver = new Augmenter().augment(driver); @@ -111,35 +118,11 @@ public class UICatalogTest { assertNotNull(file); } - @Test - @Ignore("Currently failing because no element with a tag name of 'segmentedControl' can be found") - public void testAttributes() { - //go to the toolbar section - openMenuPosition(8); - - WebElement segmented_control = driver.findElement(By.tagName("segmentedControl")); - //segmented_control is enabled by default - assertTrue(segmented_control.isEnabled()); - assertTrue(segmented_control.isDisplayed()); - //row is from previous view, should not be visible - assertFalse(row.isDisplayed()); - - WebElement tinted_switch = driver.findElements(By.tagName("switch")).get(1); - assertEquals("Tinted", tinted_switch.getText()); - //check if it is in "off" position - assertEquals(new Integer(0), Integer.valueOf(tinted_switch.getAttribute("value"))); - tinted_switch.click(); - //check if it is in "on" position - assertEquals(new Integer(1), Integer.valueOf(tinted_switch.getAttribute("value"))); - //segmented_control should now be disabled - assertFalse(segmented_control.isEnabled()); - } - @Test public void testTextFieldEdit() { //go to the text fields section openMenuPosition(2); - WebElement text_field = driver.findElements(By.tagName("textField")).get(0); + WebElement text_field = driver.findElementsByClassName("UIATextField").get(0); //get default/empty text String default_val = text_field.getAttribute("value"); //write some random text to element @@ -162,14 +145,13 @@ public class UICatalogTest { public void testAlertInteraction() { //go to the alerts section openMenuPosition(10); - List elements = driver.findElements(By.tagName("staticText")); //trigger modal alert with cancel & ok buttons - WebElement triggerOkCancel = elements.get(24); - triggerOkCancel.click(); + List triggerOkCancel = driver.findElementsByAccessibilityId("Show OK-Cancel"); + triggerOkCancel.get(1).click(); Alert alert = driver.switchTo().alert(); //check if title of alert is correct - assertEquals("UIAlertView", alert.getText()); + assertEquals("UIAlertView ", alert.getText()); alert.accept(); } @@ -177,11 +159,11 @@ public class UICatalogTest { public void testScroll() { //scroll menu //get initial third row location - row = driver.findElements(By.tagName("tableCell")).get(2); + row = driver.findElementsByClassName("UIATableCell").get(2); Point location1 = row.getLocation(); + Point center = getCenter(row); //perform swipe gesture - TouchActions swipe = new TouchActions(driver).flick(0, -20); - swipe.perform(); + driver.swipe(center.getX(), center.getY(), center.getX(), center.getY()-20, 1); //get new row coordinates Point location2 = row.getLocation(); assertEquals(location1.getX(), location2.getX()); @@ -190,33 +172,32 @@ public class UICatalogTest { @Test public void testSlider() { - //go to controls - openMenuPosition(1); - //get the slider - WebElement slider = driver.findElement(By.tagName("slider")); - assertEquals("50%", slider.getAttribute("value")); - TouchActions drag = new TouchActions(driver).flick(slider, new Integer(-1), 0, 0); - drag.perform(); - assertEquals("0%", slider.getAttribute("value")); + //go to controls + openMenuPosition(1); + //get the slider + WebElement slider = driver.findElementByClassName("UIASlider"); + assertEquals("50%", slider.getAttribute("value")); + Point sliderLocation = getCenter(slider); + driver.swipe(sliderLocation.getX(), sliderLocation.getY(), sliderLocation.getX()-100, sliderLocation.getY(), 1); + assertEquals("0%", slider.getAttribute("value")); } @Test public void testSessions() throws Exception { + HttpGet request = new HttpGet("http://localhost:4723/wd/hub/sessions"); + HttpClient httpClient = new DefaultHttpClient(); + HttpResponse response = httpClient.execute(request); + HttpEntity entity = response.getEntity(); + JSONObject jsonObject = (JSONObject) new JSONParser().parse(EntityUtils.toString(entity)); - HttpGet request = new HttpGet("http://localhost:4723/wd/hub/sessions"); - HttpClient httpClient = new DefaultHttpClient(); - HttpResponse response = httpClient.execute(request); - HttpEntity entity = response.getEntity(); - JSONObject jsonObject = (JSONObject) new JSONParser().parse(EntityUtils.toString(entity)); - - String sessionId = ((RemoteWebDriver) driver).getSessionId().toString(); - assertEquals(jsonObject.get("sessionId"), sessionId); + String sessionId = driver.getSessionId().toString(); + assertEquals(jsonObject.get("sessionId"), sessionId); } @Test public void testSize() { - Dimension table = driver.findElement(By.tagName("tableView")).getSize(); - Dimension cell = driver.findElements(By.tagName("tableCell")).get(0).getSize(); + Dimension table = driver.findElementByClassName("UIATableView").getSize(); + Dimension cell = driver.findElementsByClassName("UIATableCell").get(0).getSize(); assertEquals(table.getWidth(), cell.getWidth()); assertNotSame(table.getHeight(), cell.getHeight()); } @@ -236,17 +217,4 @@ public class UICatalogTest { assertNotSame(source_main, source_textfields); } - - 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; - } - } } diff --git a/sample-code/examples/java/testng/README.md b/sample-code/examples/java/testng/README.md deleted file mode 100644 index 82fb38f10..000000000 --- a/sample-code/examples/java/testng/README.md +++ /dev/null @@ -1,18 +0,0 @@ -Sample Appium TestNG project ---- - -This contains the source code for running sample [Appium](http://github.com/appium/appium) tests using [TestNG](http://www.testng.org). - -In order to run the tests, you will need to install [Apache Maven](http://maven.apache.org), and Appium (according to the Appium [installation instructions](https://github.com/appium/appium). - -You will then need to start appium, eg: - - grunt appium - -To compile and run all tests, run: - - mvn test - -To run a single test, run: - - mvn -Dtest=com.saucelabs.appium.SimpleTest test \ No newline at end of file diff --git a/sample-code/examples/java/testng/pom.xml b/sample-code/examples/java/testng/pom.xml deleted file mode 100644 index 4a40f5eda..000000000 --- a/sample-code/examples/java/testng/pom.xml +++ /dev/null @@ -1,78 +0,0 @@ - - - 4.0.0 - com.saucelabs - sauce_appium_testng - 0.0.1-SNAPSHOT - sauce_appium_testng - Sample Appium tests using TestNG - - - org.testng - testng - 6.8 - - - - org.seleniumhq.selenium - selenium-java - LATEST - - - - com.googlecode.json-simple - json-simple - 1.1 - - - - commons-lang - commons-lang - 2.6 - - - - log4j - log4j - 1.2.16 - - - com.saucelabs - sauce_testng - 1.0.19 - test - - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - maven-compiler-plugin - - 1.5 - 1.5 - - - - - - - - saucelabs-repository - https://repository-saucelabs.forge.cloudbees.com/release - - true - - - true - - - - - diff --git a/sample-code/examples/java/testng/src/main/java/com/example/LocalWebDriver.java b/sample-code/examples/java/testng/src/main/java/com/example/LocalWebDriver.java deleted file mode 100644 index c2b1d8ca1..000000000 --- a/sample-code/examples/java/testng/src/main/java/com/example/LocalWebDriver.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.example; - -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.HashMap; -import java.util.Map; - -import org.apache.log4j.Logger; -import org.openqa.selenium.Platform; -import org.openqa.selenium.remote.Command; -import org.openqa.selenium.remote.CommandInfo; -import org.openqa.selenium.remote.DesiredCapabilities; -import org.openqa.selenium.remote.HttpCommandExecutor; -import org.openqa.selenium.remote.HttpVerb; -import org.openqa.selenium.remote.RemoteWebDriver; -import org.openqa.selenium.remote.Response; -import org.openqa.selenium.WebDriverException; - -public class LocalWebDriver { - - //Set constants - private HttpCommandExecutor executor = null; - private DesiredCapabilities desired = null; - private URL seleniumServerURL = null; - private RemoteWebDriver remoteWebDriver = null; - private Logger logger = Logger.getLogger("WEBDRIVER"); - - /** - * Constructor: - * - * When constructing the remotewebdriver we have to set launch to false. - */ - public LocalWebDriver(URL url, DesiredCapabilities desired){ - this.desired = desired; - //setting the launch to false will stop appium from automatically launching the app. - this.desired.setCapability("launch", false); - this.remoteWebDriver = new RemoteWebDriver(url, this.desired); - } - - - /** - * This method will grab the app path from the config and install the app on the attached - * iOS device. Please make sure the appPath is accessible by the appium server. - * - * Please make sure to set the app path using the setApp method. - */ - public void installApp(){ - if (desired.getCapability("app") != null){ - try { - Object result = remoteWebDriver.executeScript("mobile: installApp"); - logger.info(result.toString()); - } catch (WebDriverException e) { - logger.error(e.getMessage()); - } - } else { - logger.error("You have to supply the app parameter before calling the install app."); - } - } - - /** - * This method will use the appPath parameter to install the app on the attached - * iOS device. Please make sure the appPath is accessible by the appium server. - * - * @param appPath to a local (zipped) app file or a url for a zipped app file. - */ - public void installApp(String appPath){ - try { - Map args = new HashMap(); - args.put("appPath", appPath); - Object result = remoteWebDriver.executeScript("mobile: installApp", args); - logger.info(result.toString()); - } catch (WebDriverException e) { - logger.error(e.getMessage()); - } - } - - /** - * This method will launch the app using mobile command - */ - public void launchApp(){ - try { - Object result = remoteWebDriver.executeScript("mobile: launchApp"); - logger.info(result.toString()); - } catch (WebDriverException e) { - logger.error(e.getMessage()); - } - } - - /** - * This method will close the app using mobile command - */ - public void closeApp(){ - try { - Object result = remoteWebDriver.executeScript("mobile: closeApp"); - logger.info(result.toString()); - } catch (WebDriverException e) { - logger.error(e.getMessage()); - } - } - - /** - * This method will use the bundlId parameter to un-install the app from - * the attached iOS device. - * - * @param bundleId of the app you would like to remove (e.g. com.gamesys.jackpotjoy) - */ - public void unInstallApp(String bundleId){ - try { - Map args = new HashMap(); - args.put("bundleId", bundleId); - Object result = remoteWebDriver.executeScript("mobile: removeApp", args); - logger.info(result.toString()); - } catch (WebDriverException e) { - logger.error(e.getMessage()); - } - } - - /** - * This method will use the bundleId parameter to check if the app is - * installed on the attached iOS device - * - * @param bundleId of the app you would like to check for (e.g. com.gamesys.jackpotjoy) - * - * @return boolean true if the app is installed and false if its not. - */ - public boolean isAppInstalled(String bundleId){ - try { - Map args = new HashMap(); - args.put("bundleId", bundleId); - Object result = remoteWebDriver.executeScript("mobile: isAppInstalled", args); - if (result.toString().toLowerCase().equals("true")) { - return true; - } else if (result.toString().toLowerCase().equals("false")) { - return false; - } else { - logger.info(result.toString()); - return false; - } - } catch (WebDriverException e) { - logger.error(e.getMessage()); - } - return false; - } -} \ No newline at end of file diff --git a/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/AndroidContactsTest.java b/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/AndroidContactsTest.java deleted file mode 100644 index 58192e678..000000000 --- a/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/AndroidContactsTest.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.saucelabs.appium; - -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 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 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; - } - } - - -} - diff --git a/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/AndroidTest.java b/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/AndroidTest.java deleted file mode 100644 index 9770cca36..000000000 --- a/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/AndroidTest.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.saucelabs.appium; - -import junit.framework.Assert; -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 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 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; - } - } - -} - diff --git a/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/AndroidWebViewTest.java b/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/AndroidWebViewTest.java deleted file mode 100644 index f1238b52e..000000000 --- a/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/AndroidWebViewTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.saucelabs.appium; - -import org.openqa.selenium.*; -import org.openqa.selenium.interactions.HasTouchScreen; -import org.openqa.selenium.interactions.TouchScreen; -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 app = new File(classpathRoot, "../../../apps/selendroid-test-app.apk"); - 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); - } - - @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; - } - } - -} diff --git a/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/SauceTest.java b/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/SauceTest.java deleted file mode 100644 index b9b6eb6c4..000000000 --- a/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/SauceTest.java +++ /dev/null @@ -1,127 +0,0 @@ -package com.saucelabs.appium; - - -import com.saucelabs.common.SauceOnDemandAuthentication; -import com.saucelabs.common.SauceOnDemandSessionIdProvider; -import com.saucelabs.testng.SauceOnDemandAuthenticationProvider; -import com.saucelabs.testng.SauceOnDemandTestListener; -import org.apache.commons.lang.StringUtils; -import org.openqa.selenium.By; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.WebElement; -import org.openqa.selenium.remote.CapabilityType; -import org.openqa.selenium.remote.DesiredCapabilities; -import org.openqa.selenium.remote.RemoteWebDriver; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Listeners; -import org.testng.annotations.Test; - -import java.net.URL; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; - -import static org.junit.Assert.assertEquals; - -/** - * Simple test which demonstrates how to run an Appium - * using Sauce Labs. - *

- * The test relies on SAUCE_USER_NAME and SAUCE_ACCESS_KEY environment variables being set which reference - * the Sauce username/access key. - * - * @author Ross Rowe - */ -@Listeners({SauceOnDemandTestListener.class}) -public class SauceTest implements SauceOnDemandSessionIdProvider, SauceOnDemandAuthenticationProvider { - - private WebDriver driver; - - private List values; - - public SauceOnDemandAuthentication authentication; - - private static final int MINIMUM = 0; - private static final int MAXIMUM = 10; - private String sessionId; - - /** - * Sets up appium. You will need to either explictly set the sauce username/access key variables, or set - * SAUCE_USER_NAME or SAUCE_USER_NAME environment variables to reference your Sauce account details. - * - * @throws Exception - */ - @BeforeMethod - public void setUp() throws Exception { - // set up appium - String username = System.getenv("SAUCE_USER_NAME"); - String key = System.getenv("SAUCE_ACCESS_KEY"); - if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(key)) { - authentication = new SauceOnDemandAuthentication(username, key); - } else { - authentication = new SauceOnDemandAuthentication(); - } - - DesiredCapabilities capabilities = new DesiredCapabilities(); - capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS 6.0"); - capabilities.setCapability("device", "iPhone Simulator"); - capabilities.setCapability(CapabilityType.PLATFORM, "Mac 10.8"); - capabilities.setCapability("app", "http://appium.s3.amazonaws.com/TestApp6.0.app.zip"); - - driver = new RemoteWebDriver(new URL(MessageFormat.format("http://{0}:{1}@ondemand.saucelabs.com:80/wd/hub", authentication.getUsername(), authentication.getAccessKey())), - capabilities); - sessionId = ((RemoteWebDriver)driver).getSessionId().toString(); - values = new ArrayList(); - } - - /** - * {@inheritDoc} - * @return - */ - @Override - public String getSessionId() { - return sessionId; - } - - @AfterMethod - public void tearDown() throws Exception { - driver.quit(); - } - - - private void populate() { - //populate text fields with two random number - List elems = driver.findElements(By.tagName("textField")); - Random random = new Random(); - - for (WebElement elem : elems) { - int rndNum = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; - elem.sendKeys(String.valueOf(rndNum)); - values.add(rndNum); - } - } - - @Test - public void testUIComputation() throws Exception { - - // populate text fields with values - populate(); - // trigger computation by using the button - WebElement button = driver.findElement(By.tagName("button")); - button.click(); - // is sum equal ? - WebElement texts = driver.findElement(By.tagName("staticText")); - assertEquals(texts.getText(), String.valueOf(values.get(0) + values.get(1))); - } - - /** - * {@inheritDoc} - * @return - */ - @Override - public SauceOnDemandAuthentication getAuthentication() { - return authentication; - } -} diff --git a/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/SimpleTest.java b/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/SimpleTest.java deleted file mode 100644 index 535dc9d67..000000000 --- a/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/SimpleTest.java +++ /dev/null @@ -1,334 +0,0 @@ -package com.saucelabs.appium; - -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.apache.http.util.EntityUtils; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.DefaultHttpClient; -import org.openqa.selenium.Dimension; -import org.openqa.selenium.Alert; -import org.openqa.selenium.By; -import org.openqa.selenium.JavascriptExecutor; -import org.openqa.selenium.WebDriver; -import org.openqa.selenium.WebElement; -import org.openqa.selenium.Point; -import org.openqa.selenium.Capabilities; -import org.openqa.selenium.remote.CapabilityType; -import org.openqa.selenium.remote.DesiredCapabilities; -import org.openqa.selenium.remote.RemoteWebDriver; -import org.openqa.selenium.remote.RemoteTouchScreen; -import org.openqa.selenium.interactions.Actions; -import org.openqa.selenium.interactions.HasTouchScreen; -import org.openqa.selenium.interactions.TouchScreen; -import org.openqa.selenium.interactions.touch.TouchActions; - -import java.io.File; -import java.net.URL; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Random; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * Simple Appium test which runs against a local Appium instance deployed - * with the 'TestApp' iPhone project which is included in the Appium source distribution. - * - * @author Ross Rowe - */ -public class SimpleTest { - - private WebDriver driver; - - private List values; - - private static final int MINIMUM = 0; - private static final int MAXIMUM = 10; - - @BeforeMethod - public void setUp() throws Exception { - // set up appium - File classpathRoot = new File(System.getProperty("user.dir")); - File appDir = new File(classpathRoot, "../../../apps/TestApp/build/Release-iphonesimulator"); - File app = new File(appDir, "TestApp.app"); - DesiredCapabilities capabilities = new DesiredCapabilities(); - capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS"); - capabilities.setCapability(CapabilityType.VERSION, "6.0"); - capabilities.setCapability(CapabilityType.PLATFORM, "Mac"); - capabilities.setCapability("app", app.getAbsolutePath()); - driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); - values = new ArrayList(); - } - - @AfterMethod - public void tearDown() throws Exception { - driver.quit(); - } - - - private void populate() { - //populate text fields with two random number - List elems = driver.findElements(By.tagName("textField")); - Random random = new Random(); - for (WebElement elem : elems) { - int rndNum = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; - elem.sendKeys(String.valueOf(rndNum)); - values.add(rndNum); - } - } - - @Test - public void testUIComputation() throws Exception { - - // populate text fields with values - populate(); - // trigger computation by using the button - WebElement button = driver.findElement(By.tagName("button")); - button.click(); - // is sum equal ? - WebElement texts = driver.findElement(By.tagName("staticText")); - assertEquals(texts.getText(), String.valueOf(values.get(0) + values.get(1))); - } - - @Test - public void testActive() throws Exception { - WebElement text = driver.findElement(By.xpath("//textfield[1]")); - assertTrue(text.isDisplayed()); - - WebElement button = driver.findElement(By.xpath("//button[1]")); - assertTrue(button.isDisplayed()); - } - - @Test - public void testBasicAlert() throws Exception { - driver.findElement(By.xpath("//button[2]")).click(); - - Alert alert = driver.switchTo().alert(); - //check if title of alert is correct - assertEquals(alert.getText(), "Cool title"); - alert.accept(); - } - - @Test - public void testBasicTagName() throws Exception { - WebElement text = driver.findElement(By.xpath("//textfield[1]")); - assertEquals(text.getTagName(), "UIATextField"); - } - - @Test - public void testBasicButton() throws Exception { - WebElement button = driver.findElement(By.xpath("//button[1]")); - assertEquals(button.getText(), "ComputeSumButton"); - } - - @Test - public void testClear() throws Exception { - WebElement text = driver.findElement(By.xpath("//textfield[1]")); - text.sendKeys("12"); - text.clear(); - - assertEquals(text.getText(), ""); - } - - @Test - public void testHideKeyboard() throws Exception { - driver.findElement(By.xpath("//textfield[1]")).sendKeys("12"); - - WebElement button = driver.findElement(By.name("Done")); - assertTrue(button.isDisplayed()); - - button.click(); - } - - @Test - public void testFindElementByTagName() throws Exception { - Random random = new Random(); - - WebElement text = driver.findElement(By.tagName("textField")); - int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; - text.sendKeys(String.valueOf(number)); - - driver.findElement(By.tagName("button")).click(); - - // is sum equal ? - WebElement sumLabel = driver.findElement(By.tagName("staticText")); - assertEquals(sumLabel.getText(), String.valueOf(number)); - } - - @Test - public void testFindElementsByTagName() throws Exception { - Random random = new Random(); - - WebElement text = driver.findElements(By.tagName("textField")).get(1); - - int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; - text.sendKeys(String.valueOf(number)); - - driver.findElements(By.tagName("button")).get(0).click(); - - // is sum equal ? - WebElement texts = driver.findElements(By.tagName("staticText")).get(0); - assertEquals(texts.getText(), String.valueOf(number)); - } - - @Test - public void testFindElementByName() throws Exception { - Random random = new Random(); - - WebElement text = driver.findElement(By.name("TextField1")); - - int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; - text.sendKeys(String.valueOf(number)); - - // is sum equal ? - WebElement sumLabel = driver.findElement(By.name("SumLabel")); - driver.findElement(By.name("ComputeSumButton")).click(); - - assertEquals(sumLabel.getText(), String.valueOf(number)); - } - - @Test - public void testFindElementsByName() throws Exception { - Random random = new Random(); - - WebElement text = driver.findElements(By.name("TextField1")).get(0); - - int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; - text.sendKeys(String.valueOf(number)); - - // is sum equal ? - WebElement sumLabel = driver.findElements(By.name("SumLabel")).get(0); - driver.findElements(By.name("ComputeSumButton")).get(0).click(); - - assertEquals(sumLabel.getText(), String.valueOf(number)); - } - - @Test - public void testFindElementByXpath() throws Exception { - Random random = new Random(); - - WebElement text = driver.findElement(By.xpath("//textfield[1]")); - - int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; - text.sendKeys(String.valueOf(number)); - - // is sum equal ? - driver.findElement(By.xpath("//button[1]")).click(); - - WebElement sumLabel = driver.findElement(By.xpath("//text[1]")); - assertEquals(sumLabel.getText(), String.valueOf(number)); - } - - @Test - public void testFindElementsByXpath() throws Exception { - Random random = new Random(); - - WebElement text = driver.findElements(By.xpath("//textfield")).get(1); - - int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; - text.sendKeys(String.valueOf(number)); - - // is sum equal ? - driver.findElements(By.xpath("//button")).get(0).click(); - - WebElement sumLabel = driver.findElements(By.xpath("//text")).get(0); - assertEquals(sumLabel.getText(), String.valueOf(number)); - } - - @Test - public void testAttribute() throws Exception { - Random random = new Random(); - - WebElement text = driver.findElement(By.xpath("//textfield[1]")); - - int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM; - text.sendKeys(String.valueOf(number)); - - assertEquals(text.getAttribute("name"), "TextField1"); - assertEquals(text.getAttribute("label"), "TextField1"); - assertEquals(text.getAttribute("value"), String.valueOf(number)); - } - - @Test - public void testSlider() throws Exception { - //get the slider - WebElement slider = driver.findElement(By.xpath("//slider[1]")); - assertEquals(slider.getAttribute("value"), "50%"); - TouchActions drag = new TouchActions(driver).flick(slider, new Integer(-1), 0, 0); - drag.perform(); - assertEquals(slider.getAttribute("value"), "0%"); - } - - @Test - public void testLocation() throws Exception { - WebElement button = driver.findElement(By.xpath("//button[1]")); - - Point location = button.getLocation(); - - assertEquals(location.getX(), 94); - assertEquals(location.getY(), 122); - } - - @Test - public void testSessions() throws Exception { - HttpGet request = new HttpGet("http://localhost:4723/wd/hub/sessions"); - HttpClient httpClient = new DefaultHttpClient(); - HttpResponse response = httpClient.execute(request); - HttpEntity entity = response.getEntity(); - JSONObject jsonObject = (JSONObject) new JSONParser().parse(EntityUtils.toString(entity)); - - String sessionId = ((RemoteWebDriver) driver).getSessionId().toString(); - assertEquals(sessionId, jsonObject.get("sessionId")); - } - - @Test - public void testSize() { - Dimension text1 = driver.findElement(By.xpath("//textfield[1]")).getSize(); - Dimension text2 = driver.findElement(By.xpath("//textfield[2]")).getSize(); - assertEquals(text1.getWidth(), text2.getWidth()); - assertEquals(text1.getHeight(), text2.getHeight()); - }@Test - @SuppressWarnings("serial") - public void testRotation() throws Exception { - WebElement button = driver.findElement(By.name("Test Gesture")); - button.click(); - ((JavascriptExecutor) driver).executeScript("mobile: rotate", new HashMap() {{ put("x", (double)114); put("y", (double)198); put("radius", (double)3); put("touchCount", (double)2); put("duration", 5.0); put("rotation", 220.0); }}); - } - - @Test - @SuppressWarnings("serial") - public void testPinchClose() throws Exception { - WebElement button = driver.findElement(By.name("Test Gesture")); - button.click(); - ((JavascriptExecutor) driver).executeScript("mobile: pinchClose", new HashMap() {{ put("startX", (double)150); put("startY", (double)230); put("endX", (double)200); put("endY", (double)260); put("duration", 2.0); }}); - } - - @Test - @SuppressWarnings("serial") - public void testPinchOpen() throws Exception { - WebElement button = driver.findElement(By.name("Test Gesture")); - button.click(); - ((JavascriptExecutor) driver).executeScript("mobile: pinchOpen", new HashMap() {{ put("startX", (double)114); put("startY", (double)198); put("endX", (double)257); put("endY", (double)256); put("duration", 2.0); }}); - } - - 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; - } - } -} diff --git a/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/UICatalogTest.java b/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/UICatalogTest.java deleted file mode 100644 index a171eddaf..000000000 --- a/sample-code/examples/java/testng/src/test/java/com/saucelabs/appium/UICatalogTest.java +++ /dev/null @@ -1,248 +0,0 @@ -package com.saucelabs.appium; - -import org.apache.commons.lang.RandomStringUtils; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.util.EntityUtils; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.openqa.selenium.*; -import org.openqa.selenium.interactions.Actions; -import org.openqa.selenium.interactions.HasTouchScreen; -import org.openqa.selenium.interactions.TouchScreen; -import org.openqa.selenium.interactions.touch.TouchActions; -import org.openqa.selenium.remote.*; -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; - -import static org.junit.Assert.*; - -/** - * Appium test which runs against a local Appium instance deployed - * with the 'UICatalog' iPhone project which is included in the Appium source distribution. - * - * @author Ross Rowe - */ -public class UICatalogTest { - - private WebDriver driver; - - private WebElement row; - - @BeforeMethod - public void setUp() throws Exception { - // set up appium - File classpathRoot = new File(System.getProperty("user.dir")); - File appDir = new File(classpathRoot, "../../../apps/UICatalog/build/Release-iphonesimulator"); - File app = new File(appDir, "UICatalog.app"); - DesiredCapabilities capabilities = new DesiredCapabilities(); - capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS"); - capabilities.setCapability(CapabilityType.VERSION, "6.0"); - capabilities.setCapability(CapabilityType.PLATFORM, "Mac"); - capabilities.setCapability("app", app.getAbsolutePath()); - driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); - } - - @AfterMethod - public void tearDown() throws Exception { - driver.quit(); - } - - private void openMenuPosition(int index) { - //populate text fields with two random number - WebElement table = driver.findElement(By.tagName("tableView")); - row = table.findElements(By.tagName("tableCell")).get(index); - row.click(); - } - - @Test - public void testFindElement() throws Exception { - //first view in UICatalog is a table - WebElement table = driver.findElement(By.tagName("tableView")); - assertNotNull(table); - //is number of cells/rows inside table correct - List rows = table.findElements(By.tagName("tableCell")); - assertEquals(12, rows.size()); - //is first one about buttons - assertEquals(rows.get(0).getAttribute("name"), "Buttons, Various uses of UIButton"); - //navigationBar is not inside table - WebElement nav_bar = null; - try { - nav_bar = table.findElement(By.tagName("navigationBar")); - } catch (NoSuchElementException e) { - //expected - } - assertNull(nav_bar); - //there is nav bar inside the app - driver.getPageSource(); - nav_bar = driver.findElement(By.tagName("navigationBar")); - assertNotNull(nav_bar); - } - - - @Test - public void test_location() { - //get third row location - row = driver.findElements(By.tagName("tableCell")).get(2); - assertEquals(row.getLocation().getX(), 0); - assertEquals(row.getLocation().getY(), 152); - } - - @Test(enabled = false) - public void testScreenshot() { - //make screenshot and get is as base64 - WebDriver augmentedDriver = new Augmenter().augment(driver); - String screenshot = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.BASE64); - - assertNotNull(screenshot); - //make screenshot and save it to the local filesystem - File file = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE); - assertNotNull(file); - } - - @Test(enabled = false) - public void testAttributes() { - //go to the toolbar section - openMenuPosition(8); - - WebElement segmented_control = driver.findElement(By.tagName("segmentedControl")); - //segmented_control is enabled by default - assertTrue(segmented_control.isEnabled()); - assertTrue(segmented_control.isDisplayed()); - //row is from previous view, should not be visible - assertFalse(row.isDisplayed()); - - WebElement tinted_switch = driver.findElements(By.tagName("switch")).get(1); - assertEquals(tinted_switch.getText(), "Tinted"); - //check if it is in "off" position - assertEquals(Integer.valueOf(tinted_switch.getAttribute("value")), new Integer(0)); - tinted_switch.click(); - //check if it is in "on" position - assertEquals(Integer.valueOf(tinted_switch.getAttribute("value")), new Integer(1)); - //segmented_control should now be disabled - assertFalse(segmented_control.isEnabled()); - } - - @Test - public void testTextFieldEdit() { - //go to the text fields section - openMenuPosition(2); - WebElement text_field = driver.findElements(By.tagName("textField")).get(0); - //get default/empty text - String default_val = text_field.getAttribute("value"); - //write some random text to element - String rnd_string = RandomStringUtils.randomAlphanumeric(6); - text_field.sendKeys(rnd_string); - assertEquals(text_field.getAttribute("value"), rnd_string); - //send some random keys - String rnd_string2 = RandomStringUtils.randomAlphanumeric(6); - Actions swipe = new Actions(driver).sendKeys(rnd_string2); - swipe.perform(); - //check if text is there - assertEquals(text_field.getAttribute("value"), rnd_string + rnd_string2); - //clear - text_field.clear(); - //check if is empty/has default text - assertEquals(text_field.getAttribute("value"), default_val); - } - - @Test - public void testAlertInteraction() { - //go to the alerts section - openMenuPosition(10); - List elements = driver.findElements(By.tagName("staticText")); - - //trigger modal alert with cancel & ok buttons - WebElement triggerOkCancel = elements.get(24); - triggerOkCancel.click(); - Alert alert = driver.switchTo().alert(); - //check if title of alert is correct - assertEquals(alert.getText(), "UIAlertView"); - alert.accept(); - } - - @Test - public void testScroll() { - //scroll menu - //get initial third row location - row = driver.findElements(By.tagName("tableCell")).get(2); - Point location1 = row.getLocation(); - //perform swipe gesture - TouchActions swipe = new TouchActions(driver).flick(0, -20); - swipe.perform(); - //get new row coordinates - Point location2 = row.getLocation(); - assertEquals(location1.getX(), location2.getX()); - assertNotSame(location1.getY(), location2.getY()); - } - - @Test - public void testSlider() { - //go to controls - openMenuPosition(1); - //get the slider - WebElement slider = driver.findElement(By.tagName("slider")); - assertEquals(slider.getAttribute("value"), "50%"); - TouchActions drag = new TouchActions(driver).flick(slider, new Integer(-1), 0, 0); - drag.perform(); - assertEquals(slider.getAttribute("value"), "0%"); - } - - @Test - public void testSessions() throws Exception { - - HttpGet request = new HttpGet("http://localhost:4723/wd/hub/sessions"); - HttpClient httpClient = new DefaultHttpClient(); - HttpResponse response = httpClient.execute(request); - HttpEntity entity = response.getEntity(); - JSONObject jsonObject = (JSONObject) new JSONParser().parse(EntityUtils.toString(entity)); - - String sessionId = ((RemoteWebDriver) driver).getSessionId().toString(); - assertEquals(sessionId, jsonObject.get("sessionId")); - } - - @Test - public void testSize() { - Dimension table = driver.findElement(By.tagName("tableView")).getSize(); - Dimension cell = driver.findElements(By.tagName("tableCell")).get(0).getSize(); - assertEquals(table.getWidth(), cell.getWidth()); - assertNotSame(table.getHeight(), cell.getHeight()); - } - - @Test - public void testSource() { - //get main view soruce - String source_main = driver.getPageSource(); - assertTrue(source_main.contains("UIATableView")); - assertTrue(source_main.contains("TextFields, Uses of UITextField")); - - //got to text fields section - openMenuPosition(2); - String source_textfields = driver.getPageSource(); - assertTrue(source_textfields.contains("UIAStaticText")); - assertTrue(source_textfields.contains("TextFields")); - - assertNotSame(source_main, source_textfields); - } - - 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; - } - } -}