1baa30b35081c36876a7d220184caae49fc40591
[pub/Android/ownCloud.git] / automationTest / src / test / java / com / owncloud / android / test / ui / testSuites / Common.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author purigarcia
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 package com.owncloud.android.test.ui.testSuites;
22
23 import static org.junit.Assert.*;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.net.URL;
28 import java.text.SimpleDateFormat;
29 import java.util.Calendar;
30 import java.util.Date;
31 import java.util.concurrent.TimeUnit;
32
33 import org.apache.commons.io.FileUtils;
34 import org.openqa.selenium.By;
35 import org.openqa.selenium.NoSuchElementException;
36 import org.openqa.selenium.OutputType;
37 import org.openqa.selenium.TimeoutException;
38 import org.openqa.selenium.remote.DesiredCapabilities;
39 import org.openqa.selenium.remote.RemoteWebDriver;
40 import org.openqa.selenium.support.ui.WebDriverWait;
41
42 import io.appium.java_client.android.AndroidDriver;
43 import io.appium.java_client.android.AndroidElement;
44
45 public class Common{
46 AndroidDriver driver;
47 static int waitingTime = 30;
48
49 public WebDriverWait wait;
50
51 protected AndroidDriver setUpCommonDriver () throws Exception {
52 File rootPath = new File(System.getProperty("user.dir"));
53 File appDir = new File(rootPath,"src/test/resources");
54 File app = new File(appDir,"ownCloud.apk");
55 DesiredCapabilities capabilities = new DesiredCapabilities();
56 capabilities.setCapability("platformName", "Android");
57 capabilities.setCapability("deviceName", "test");
58 capabilities.setCapability("app", app.getAbsolutePath());
59 capabilities.setCapability("appPackage", "com.owncloud.android");
60 capabilities.setCapability("appActivity",
61 ".ui.activity.FileDisplayActivity");
62 capabilities.setCapability("appWaitActivity",
63 ".authentication.AuthenticatorActivity");
64 driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
65 capabilities);
66 driver.manage().timeouts().implicitlyWait(waitingTime,
67 TimeUnit.SECONDS);
68 wait = new WebDriverWait(driver, waitingTime, 50);
69 return driver;
70
71 }
72
73 protected boolean waitForTextPresent(String text, AndroidElement element)
74 throws InterruptedException{
75 for (int second = 0;;second++){
76 if (second >= waitingTime)
77 return false;
78 try{
79 if (text.equals(element.getText()))
80 break;
81 } catch (Exception e){
82
83 }
84 Thread.sleep(1000);
85 }
86 return true;
87 }
88
89 protected boolean isElementPresent(AndroidElement element, By by) {
90 try {
91 element.findElement(by);
92 return true;
93 } catch (NoSuchElementException e) {
94 return false;
95 }
96 }
97
98 public static boolean isElementPresent(AndroidElement element) {
99 try{
100 element.isDisplayed();
101 } catch (NoSuchElementException e){
102 return false;
103 }
104 return true;
105 }
106
107 //pollingTime in milliseconds
108 public static void waitTillElementIsNotPresent (AndroidElement element,
109 int pollingTime) throws Exception {
110 for (int time = 0;time <= waitingTime * 1000;time += pollingTime){
111 try{
112 element.isDisplayed();
113 } catch (NoSuchElementException e){
114 return;
115 }
116 Thread.sleep(pollingTime);
117 }
118 throw new TimeoutException();
119 }
120
121 public static void waitTillElementIsNotPresentWithoutTimeout (
122 AndroidElement element,int pollingTime)
123 throws InterruptedException {
124 for (int time = 0;time <= waitingTime * 1000;time += pollingTime){
125 try{
126 element.isDisplayed();
127 } catch (NoSuchElementException e){
128 return;
129 }
130 Thread.sleep(pollingTime);
131 }
132 }
133
134 protected void takeScreenShotOnFailed (String testName)
135 throws IOException {
136 File file = ((RemoteWebDriver) driver)
137 .getScreenshotAs(OutputType.FILE);
138 SimpleDateFormat dt1 = new SimpleDateFormat("yyyy-MM-dd");
139 Date today = Calendar.getInstance().getTime();
140 String screenShotName = "ScreenShots/" + dt1.format(today) + "/"
141 + testName + ".png";
142 FileUtils.copyFile(file, new File(screenShotName));
143 }
144
145 protected void assertIsInFileListView() throws InterruptedException {
146 assertTrue(waitForTextPresent("ownCloud", (AndroidElement) driver
147 .findElementByAndroidUIAutomator("new UiSelector()"
148 + ".resourceId(\"android:id/action_bar_title\")")));
149 assertTrue(isElementPresent((AndroidElement) driver
150 .findElementByAndroidUIAutomator("new UiSelector()"
151 + ".description(\"Upload\")")));
152 }
153
154 protected void assertIsNotInFileListView() throws InterruptedException {
155 AndroidElement fileElement;
156 assertTrue(waitForTextPresent("ownCloud", (AndroidElement) driver
157 .findElementByAndroidUIAutomator("new UiSelector()"
158 + ".resourceId(\"android:id/action_bar_title\")")));
159 try {
160 fileElement = (AndroidElement) driver
161 .findElementByAndroidUIAutomator("new UiSelector()"
162 + ".description(\"Upload\")");
163 } catch (NoSuchElementException e) {
164 fileElement = null;
165 }
166 assertNull(fileElement);
167 }
168
169 protected void assertIsPasscodeRequestView() throws InterruptedException {
170 assertTrue(waitForTextPresent("ownCloud", (AndroidElement) driver
171 .findElementByAndroidUIAutomator("new UiSelector()"
172 + ".resourceId(\"android:id/action_bar_title\")")));
173 assertTrue(((AndroidElement) driver.findElementByAndroidUIAutomator(
174 "new UiSelector().text(\"Please, insert your pass code\")"))
175 .isDisplayed());
176
177 }
178
179 protected void assertIsInSettingsView() throws InterruptedException {
180 assertTrue(waitForTextPresent("Settings", (AndroidElement) driver
181 .findElementByAndroidUIAutomator("new UiSelector()"
182 + ".resourceId(\"android:id/action_bar_title\")")));
183 }
184
185 }