|
| 1 | +import io.appium.java_client.AppiumDriver; |
| 2 | +import io.appium.java_client.MultiTouchAction; |
| 3 | +import io.appium.java_client.TouchAction; |
| 4 | +import io.appium.java_client.android.AndroidDriver; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.FileOutputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.OutputStream; |
| 10 | +import java.net.URL; |
| 11 | +import java.text.DateFormat; |
| 12 | +import java.text.SimpleDateFormat; |
| 13 | +import java.util.*; |
| 14 | + |
| 15 | +import org.apache.commons.io.FileUtils; |
| 16 | +import org.apache.commons.codec.binary.Base64; |
| 17 | +import org.junit.After; |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.Test; |
| 20 | +import org.openqa.selenium.By; |
| 21 | +import org.openqa.selenium.OutputType; |
| 22 | +import org.openqa.selenium.WebElement; |
| 23 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 24 | +import org.openqa.selenium.support.ui.ExpectedCondition; |
| 25 | +import org.openqa.selenium.support.ui.ExpectedConditions; |
| 26 | +import org.openqa.selenium.support.ui.WebDriverWait; |
| 27 | + |
| 28 | +// Aliyun test. with Appium and JDK 6 |
| 29 | +public class Main { |
| 30 | + private AppiumDriver<WebElement> driver; |
| 31 | + private static final boolean takeScreenshot = false; |
| 32 | + |
| 33 | + @Before |
| 34 | + public void setUp() throws Exception { |
| 35 | + // set up appium |
| 36 | + DesiredCapabilities capabilities = Capabilities.getCapabilities(); |
| 37 | + driver = new AndroidDriver<WebElement>(new URL(Capabilities.getUrl()), capabilities); |
| 38 | + } |
| 39 | + |
| 40 | + @After |
| 41 | + public void tearDown() throws Exception { |
| 42 | + driver.quit(); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + @Test |
| 47 | + public void uiWalkThrough() { |
| 48 | + waitForElementBy(By.className("android.webkit.WebView"), 5); |
| 49 | + takeScreenshot(); |
| 50 | + waitForElementBy(By.id("English")).click(); |
| 51 | + takeScreenshot(); |
| 52 | + waitForElementBy(By.id("Confirm"), 50).click(); |
| 53 | + takeScreenshot(); |
| 54 | + // Swipes |
| 55 | + wait(500); |
| 56 | + swipeLeft(); |
| 57 | + swipeLeft(); |
| 58 | + swipeLeft(); |
| 59 | + swipeRight(); |
| 60 | + swipeLeft(); |
| 61 | + swipeLeft(); |
| 62 | + swipeLeft(); |
| 63 | + swipeLeft(); |
| 64 | + swipeLeft(); |
| 65 | + swipeLeft(); |
| 66 | + swipeRight(); |
| 67 | + swipeRight(); |
| 68 | + swipeRight(); |
| 69 | + swipeRight(); |
| 70 | + swipeRight(); |
| 71 | + swipeRight(); |
| 72 | + swipeRight(); |
| 73 | + |
| 74 | + // Click card |
| 75 | + clickByCoordinatesPercentage(0.25f, 0.35f); |
| 76 | + wait(500); |
| 77 | + takeScreenshot(); |
| 78 | + clickByCoordinatesPercentage(0.25f, 0.65f); |
| 79 | + wait(1000); |
| 80 | + takeScreenshot(); |
| 81 | + wait(6000); |
| 82 | + swipeLeft(); |
| 83 | + clickByCoordinatesPercentage(0.25f, 0.35f); |
| 84 | + wait(500); |
| 85 | + takeScreenshot(); |
| 86 | + clickByCoordinatesPercentage(0.25f, 0.65f); |
| 87 | + wait(7000); |
| 88 | + |
| 89 | + // Edit mode |
| 90 | + multiTouchEnterEditMode(); |
| 91 | + takeScreenshot(); |
| 92 | + |
| 93 | + // Change courseware |
| 94 | + waitForElementBy(By.id("All")).click(); |
| 95 | + takeScreenshot(); |
| 96 | + clickCourseListItem(); |
| 97 | + wait(500); |
| 98 | + swipeLeft(); |
| 99 | + swipeLeft(); |
| 100 | + swipeLeft(); |
| 101 | + swipeRight(); |
| 102 | + swipeLeft(); |
| 103 | + swipeLeft(); |
| 104 | + |
| 105 | + // Settings |
| 106 | + waitForElementBy(By.id("Settings")).click(); |
| 107 | + takeScreenshot(); |
| 108 | + waitForElementBy(By.id("Cancel")).click(); |
| 109 | + |
| 110 | + // Change layout |
| 111 | + waitForElementBy(By.id("Settings")).click(); |
| 112 | + waitForElementBy(By.id("parent_settingspop_layout1_1")).click(); |
| 113 | + waitForElementBy(By.id("Confirm")).click(); |
| 114 | + takeScreenshot(); |
| 115 | + wait(500); |
| 116 | + swipeLeft(); |
| 117 | + swipeLeft(); |
| 118 | + swipeLeft(); |
| 119 | + swipeLeft(); |
| 120 | + swipeLeft(); |
| 121 | + swipeRight(); |
| 122 | + |
| 123 | + waitForElementBy(By.id("Settings")).click(); |
| 124 | + waitForElementBy(By.id("parent_settingspop_layout2_2")).click(); |
| 125 | + waitForElementBy(By.id("Confirm")).click(); |
| 126 | + |
| 127 | + // New courseware |
| 128 | + waitForElementBy(By.id("New")).click(); |
| 129 | + waitForElementBy(By.className("android.widget.EditText")).sendKeys("abc123"); |
| 130 | + driver.hideKeyboard(); |
| 131 | + takeScreenshot(); |
| 132 | + waitForElementBy(By.id("Confirm")).click(); |
| 133 | + |
| 134 | + // Pick |
| 135 | + wait(1000); |
| 136 | + clickByCoordinatesPercentage(0.25f, 0.35f); |
| 137 | + takeScreenshot(); |
| 138 | + waitForElementBy(By.id("Back")).click(); |
| 139 | + |
| 140 | + // Library |
| 141 | + waitForElementBy(By.id("Library")).click(); |
| 142 | + takeScreenshot(); |
| 143 | + wait(500); |
| 144 | + swipeLeft(); |
| 145 | + swipeLeft(); |
| 146 | + swipeLeft(); |
| 147 | + swipeRight(); |
| 148 | + swipeRight(); |
| 149 | + swipeLeft(); |
| 150 | + swipeLeft(); |
| 151 | + |
| 152 | + clickByCoordinatesPercentage(0.25f, 0.65f); |
| 153 | + clickByCoordinatesPercentage(0.25f, 0.65f); |
| 154 | + |
| 155 | + // New category |
| 156 | + waitForElementBy(By.id("New")).click(); |
| 157 | + wait(500); |
| 158 | + clickNewCategoryPopoverItem(); |
| 159 | + wait(1500); |
| 160 | +// waitForElementBy(By.className("android.widget.EditText")).sendKeys("category"); |
| 161 | +// driver.hideKeyboard(); |
| 162 | + takeScreenshot(); |
| 163 | + waitForElementBy(By.id("Cancel")).click(); |
| 164 | + |
| 165 | + // New card |
| 166 | + waitForElementBy(By.id("New")).click(); |
| 167 | + wait(500); |
| 168 | + clickNewCardPopoverItem(); |
| 169 | + wait(1500); |
| 170 | +// waitForElementBy(By.className("android.widget.EditText")).sendKeys("card"); |
| 171 | +// driver.hideKeyboard(); |
| 172 | + takeScreenshot(); |
| 173 | + waitForElementBy(By.id("Cancel")).click(); |
| 174 | + |
| 175 | + // Back |
| 176 | + waitForElementBy(By.id("Back")).click(); |
| 177 | + |
| 178 | + // Change courseware again |
| 179 | + clickByCoordinatesPercentage(0.5f, 0.03f); |
| 180 | + clickCourseListItem(); |
| 181 | + |
| 182 | + // Exit edit mode |
| 183 | + waitForElementBy(By.id("Done")).click(); |
| 184 | + |
| 185 | + // Swipes again |
| 186 | + wait(500); |
| 187 | + swipeLeft(); |
| 188 | + swipeLeft(); |
| 189 | + swipeLeft(); |
| 190 | + swipeRight(); |
| 191 | + swipeRight(); |
| 192 | + swipeLeft(); |
| 193 | + swipeLeft(); |
| 194 | + |
| 195 | +// randomClicks(100, 200); |
| 196 | + } |
| 197 | + |
| 198 | + |
| 199 | + private void wait(int ms) { |
| 200 | + try { |
| 201 | + Thread.sleep(ms); |
| 202 | + } catch (InterruptedException e) { |
| 203 | + e.printStackTrace(); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + private WebElement waitForElementBy(By method) { |
| 208 | + return waitForElementBy(method, 5); |
| 209 | + } |
| 210 | + |
| 211 | + private WebElement waitForElementBy(By method, int seconds) { |
| 212 | + List<WebElement> list = (new WebDriverWait(driver, seconds)). |
| 213 | + until(ExpectedConditions.visibilityOfAllElementsLocatedBy(method)); |
| 214 | + System.out.println("Found " + list.size() + " element(s)"); |
| 215 | + if(list != null && list.size() > 0) { |
| 216 | + return list.get(0); |
| 217 | + } else { |
| 218 | + throw new RuntimeException("Element not found in " + seconds + " seconds"); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + private void multiTouchEnterEditMode() { |
| 223 | + int height = driver.manage().window().getSize().getHeight(); |
| 224 | + int width = driver.manage().window().getSize().getWidth(); |
| 225 | + System.out.println("height: " + height + " width: " + width); |
| 226 | + MultiTouchAction multiTouch = new MultiTouchAction(driver); |
| 227 | + |
| 228 | + // int[x, y] |
| 229 | + ArrayList<int[]> coordinates = new ArrayList<int[]>(); |
| 230 | + coordinates.add(new int[]{10, 10}); |
| 231 | + coordinates.add(new int[]{width - 10, 10}); |
| 232 | + coordinates.add(new int[]{width - 10, height - 10}); |
| 233 | + |
| 234 | + for (int i = 0; i < 3; i++) { |
| 235 | + TouchAction tap = new TouchAction(driver); |
| 236 | + multiTouch.add(tap.press(coordinates.get(i)[0], coordinates.get(i)[1]).waitAction(100).release()); |
| 237 | + } |
| 238 | + |
| 239 | + multiTouch.perform(); |
| 240 | + } |
| 241 | + |
| 242 | + private void clickByCoordinatesPercentage(float xpercent, float ypercent) { |
| 243 | + // Popovers are reporting wrong coordinates. Use estimated coordinates instead |
| 244 | + int width = driver.manage().window().getSize().getWidth(); |
| 245 | + int height = driver.manage().window().getSize().getHeight(); |
| 246 | + wait(500); |
| 247 | + driver.tap(1, (int) (width * xpercent), (int) (height * ypercent), 100); |
| 248 | + } |
| 249 | + |
| 250 | + private void clickCourseListItem() { |
| 251 | + clickByCoordinatesPercentage(0.5f, 0.24f); |
| 252 | + } |
| 253 | + |
| 254 | + private void clickNewCardPopoverItem() { |
| 255 | + clickByCoordinatesPercentage(0.9f, 0.12f); |
| 256 | + } |
| 257 | + |
| 258 | + private void clickNewCategoryPopoverItem() { |
| 259 | + clickByCoordinatesPercentage(0.9f, 0.19f); |
| 260 | + } |
| 261 | + |
| 262 | + private void swipeLeft() { |
| 263 | + int height = driver.manage().window().getSize().getHeight(); |
| 264 | + int width = driver.manage().window().getSize().getWidth(); |
| 265 | + TouchAction swipe = new TouchAction(driver).press(width * 8 / 10, height / 2) |
| 266 | + .waitAction(100).moveTo(width * 2 / 10, height / 2).release(); |
| 267 | + swipe.perform(); |
| 268 | + wait(500); |
| 269 | + } |
| 270 | + |
| 271 | + private void swipeRight() { |
| 272 | + int height = driver.manage().window().getSize().getHeight(); |
| 273 | + int width = driver.manage().window().getSize().getWidth(); |
| 274 | + TouchAction swipe = new TouchAction(driver).press(width * 2 / 10, height / 2) |
| 275 | + .waitAction(100).moveTo(width * 8 / 10, height / 2).release(); |
| 276 | + swipe.perform(); |
| 277 | + wait(500); |
| 278 | + } |
| 279 | + |
| 280 | + private void takeScreenshot() { |
| 281 | + if(!takeScreenshot) return; |
| 282 | + String screenshootB64 = driver.getScreenshotAs(OutputType.BASE64); |
| 283 | + byte[] data = Base64.decodeBase64(screenshootB64); |
| 284 | + try { |
| 285 | + String destDir = "screenshots"; |
| 286 | + File scrFile = File.createTempFile("screenshot", ".png"); |
| 287 | + scrFile.deleteOnExit(); |
| 288 | + try (OutputStream stream = new FileOutputStream(scrFile)) { |
| 289 | + stream.write(data); |
| 290 | + } |
| 291 | + DateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd__hh_mm_ssaa"); |
| 292 | + String destFile = dateFormat.format(new Date()) + ".png"; |
| 293 | + |
| 294 | + // To create folder to store screenshots |
| 295 | + new File(destDir).mkdirs(); |
| 296 | + // Set file name with combination of test class name + date time. |
| 297 | + System.out.println(destFile); |
| 298 | + // Store file at destination folder location |
| 299 | + FileUtils.copyFile(scrFile, new File(destDir + "/" + destFile)); |
| 300 | + } catch (IOException e) { |
| 301 | + e.printStackTrace(); |
| 302 | + } |
| 303 | + } |
| 304 | + |
| 305 | + private void randomClicks(int numOfClicks, int intterval) { |
| 306 | + int height = driver.manage().window().getSize().getHeight(); |
| 307 | + int width = driver.manage().window().getSize().getWidth(); |
| 308 | + Random r = new Random(); |
| 309 | + for(int i= 0; i <= numOfClicks; i++) { |
| 310 | + TouchAction tap = new TouchAction(driver).tap(r.nextInt(width), r.nextInt(height)).waitAction(75).release(); |
| 311 | + wait(intterval); |
| 312 | + } |
| 313 | + } |
| 314 | +} |
0 commit comments