diff --git a/sample-code/examples/java/junit/pom.xml b/sample-code/examples/java/junit/pom.xml index a761f7a68..cff576e76 100644 --- a/sample-code/examples/java/junit/pom.xml +++ b/sample-code/examples/java/junit/pom.xml @@ -4,6 +4,7 @@ 4.0.0 com.saucelabs sauce_appium_junit + 0.0.1-SNAPSHOT sauce_appium_junit @@ -19,6 +20,18 @@ LATEST test + + org.seleniumhq.selenium + selenium-java + LATEST + test + + + com.googlecode.json-simple + json-simple + 1.1 + test + 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 new file mode 100644 index 000000000..fc41bc7e3 --- /dev/null +++ b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SauceTest.java @@ -0,0 +1,79 @@ +package com.saucelabs.appium; + +import org.junit.After; +import org.junit.Before; +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; +import org.openqa.selenium.remote.RemoteWebDriver; + +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; + +/** + * @author Ross Rowe + */ +public class SauceTest { + + private WebDriver driver; + + private List values; + + private static final int MINIMUM = 0; + private static final int MAXIMUM = 10; + + @Before + public void setUp() throws Exception { + // set up appium + String sauceUserName = System.getenv("SAUCE_USER_NAME"); + String sauceAccessKey = System.getenv("SAUCE_ACCESS_KEY"); + DesiredCapabilities capabilities = new DesiredCapabilities(); + capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS"); + capabilities.setCapability(CapabilityType.VERSION, "6.0"); + capabilities.setCapability(CapabilityType.PLATFORM, "Mac"); + capabilities.setCapability("app", "https://raw.github.com/appium/appium/master/assets/TestApp.app.zip"); + + driver = new RemoteWebDriver(new URL(MessageFormat.format("http://{0}:{1}@ondemand.saucelabs.com:80/wd/hub"), sauceUserName, sauceAccessKey), + capabilities); + values = new ArrayList(); + } + + @After + 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) { + String rndNum = String.valueOf(random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM); + elem.sendKeys(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(), values.get(0) + values.get(1)); + } +} 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 new file mode 100644 index 000000000..180618efb --- /dev/null +++ b/sample-code/examples/java/junit/src/test/java/com/saucelabs/appium/SimpleTest.java @@ -0,0 +1,79 @@ +package com.saucelabs.appium; + +import org.junit.After; +import org.junit.Before; +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; +import org.openqa.selenium.remote.RemoteWebDriver; + +import java.io.File; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import static org.junit.Assert.assertEquals; + +/** + * @author Ross Rowe + */ +public class SimpleTest { + + private WebDriver driver; + + private List values; + + private static final int MINIMUM = 0; + private static final int MAXIMUM = 10; + + @Before + public void setUp() throws Exception { + // set up appium + File app = new File( + "../../apps/TestApp/build/Release-iphonesimulator", + "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 RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); + values = new ArrayList(); + } + + @After + 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) { + String rndNum = String.valueOf(random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM); + elem.sendKeys(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(), values.get(0) + values.get(1)); + } + + +} 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 d477af0fa..14a43e2cf 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,7 +1,187 @@ package com.saucelabs.appium; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.Point; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; + +import java.util.List; + +import static org.junit.Assert.*; + /** * @author Ross Rowe */ public class UICatalogTest { + + private WebDriver driver; + + private WebElement row; + + 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 findElement() 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).getText(), "Buttons, Various uses of UIButton"); + //navigationBar is not inside table + WebElement nav_bar = table.findElement(By.tagName("navigationBar")); + assertNull(nav_bar); + //there is nav bar inside the app + 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 + public void testScreenshot() { + //make screenshot and get is as base64 + screenshot = driver.get_screenshot_as_base64() + self.assertTrue(screenshot) + //make screenshot and save it to the local filesystem + success = self.driver.get_screenshot_as_file("foo.png") + self.assertTrue(success) + } + + @Test + public void testAttributes() { + //go to the toolbar section + openMenuPosition(9); + + 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 = str_generator(); + text_field.sendKeys(rnd_string); + assertEquals(text_field.getAttribute("value"), rnd_string); + //send some random keys + String rnd_string2 = str_generator() + swipe = ActionChains(self.driver).send_keys(rnd_string2) + swipe.perform() + //check if text is there + assertEquals(text_field.getAttribute("value"), 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(14); + triggerOkCancel.click(); + WebElement alert = driver.switchTo(); + //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 + swipe = TouchActions(self.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%"); + drag = TouchActions(self.driver) + drag.flick_element(slider, -0.5, 0, 0) + drag.perform() + assertEquals(slider.getAttribute("value"), "0%"); + } + + @Test + public void testSessions() { + data = json.loads(urllib2.urlopen("http://localhost:4723/wd/hub/sessions").read()) + self.assertEqual(self.driver.session_id, data[0]['id']) + } + + @Test + public void testSize() { + table = driver.find_element_by_tag_name("tableView").size + row = driver.find_elements_by_tag_name("tableCell")[0].size + assertEquals(table['width'], row['width']) + assertNotSame(table['height'], row['height']) + } + + @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); + } } diff --git a/sample-code/examples/java/testng/pom.xml b/sample-code/examples/java/testng/pom.xml index 117fa5a3b..3188a479a 100644 --- a/sample-code/examples/java/testng/pom.xml +++ b/sample-code/examples/java/testng/pom.xml @@ -19,6 +19,12 @@ LATEST test + + org.seleniumhq.selenium + selenium-java + LATEST + test +