forked from JasmineChiu/sample-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af86ada
commit bced07a
Showing
9 changed files
with
303 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
...ava/junit/src/test/java/com/saucelabs/appium/AndroidPageObjectTest_TimeOutManagement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package com.saucelabs.appium; | ||
|
||
import com.saucelabs.appium.page_object.PageObjectWithCustomizedTimeOuts; | ||
import io.appium.java_client.MobileElement; | ||
import io.appium.java_client.android.AndroidDriver; | ||
import io.appium.java_client.pagefactory.AppiumFieldDecorator; | ||
import io.appium.java_client.pagefactory.TimeOutDuration; | ||
import io.appium.java_client.remote.MobileCapabilityType; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.remote.DesiredCapabilities; | ||
import org.openqa.selenium.support.PageFactory; | ||
|
||
import java.io.File; | ||
import java.net.URL; | ||
import java.util.Calendar; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* This sample just demonstrates that the time out customization/changing | ||
* works fine | ||
*/ | ||
public class AndroidPageObjectTest_TimeOutManagement { | ||
|
||
private WebDriver driver; | ||
private PageObjectWithCustomizedTimeOuts pageObjectWithCustomizedTimeOuts; | ||
private TimeOutDuration timeOutDuration; | ||
private final static long ACCEPTABLE_DELTA_MILLS = 1500; //Android UIAutomator sometimes | ||
//is very slow | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
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(MobileCapabilityType.DEVICE_NAME, "Android Emulator"); | ||
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath()); | ||
driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); | ||
timeOutDuration = new TimeOutDuration(1, TimeUnit.SECONDS); /* This object can be passed through | ||
the constructor of AppiumFieldDecorator. It allows users to set general timeout | ||
of the waiting for elements conveniently and change it when it is needed. | ||
*/ | ||
|
||
pageObjectWithCustomizedTimeOuts = new PageObjectWithCustomizedTimeOuts(); | ||
//This time out is set because test can be run on slow Android SDK emulator | ||
PageFactory.initElements(new AppiumFieldDecorator(driver, timeOutDuration), | ||
pageObjectWithCustomizedTimeOuts); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
driver.quit(); | ||
} | ||
|
||
private static void checkTimeDifference(long expectedTime, | ||
TimeUnit timeUnit, long currentMillis) { | ||
long expectedMillis = TimeUnit.MILLISECONDS.convert(expectedTime, | ||
timeUnit); | ||
try { | ||
assertEquals(true, | ||
((currentMillis - expectedMillis) < ACCEPTABLE_DELTA_MILLS) | ||
&& ((currentMillis - expectedMillis) >= 0)); | ||
} | ||
catch (Error e){ | ||
String message = String.valueOf(expectedTime) + " " + timeUnit.toString() + " current duration in millis " + | ||
String.valueOf(currentMillis) + " Failed"; | ||
throw new RuntimeException(message, e); | ||
} | ||
} | ||
|
||
private long getBenchMark(List<MobileElement> stubElements) { | ||
long startMark = Calendar.getInstance().getTimeInMillis(); | ||
stubElements.size(); | ||
long endMark = Calendar.getInstance().getTimeInMillis(); | ||
return endMark - startMark; | ||
} | ||
|
||
/** | ||
* Please read about Page Object design pattern here: | ||
* https://code.google.com/p/selenium/wiki/PageObjects | ||
*/ | ||
/** | ||
* Page Object best practice is to describe interactions with target | ||
* elements by methods. These methods describe business logic of the page/screen. | ||
* Here test interacts with lazy instantiated elements directly. | ||
* It was done so just for obviousness | ||
*/ | ||
|
||
@Test | ||
public void test() { | ||
checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT, | ||
getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements)); | ||
System.out.println(String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT) | ||
+ " " + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine"); | ||
|
||
timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS); | ||
checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements)); | ||
System.out.println("Change time: " + String.valueOf(15500000) + " " | ||
+ TimeUnit.MICROSECONDS.toString() + ": Fine"); | ||
|
||
timeOutDuration.setTime(3, TimeUnit.SECONDS); | ||
checkTimeDifference(3, TimeUnit.SECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements)); | ||
System.out.println("Change time: " + String.valueOf(3) + " " | ||
+ TimeUnit.SECONDS.toString() + ": Fine"); | ||
} | ||
|
||
@Test | ||
public void test2() { | ||
checkTimeDifference(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT, AppiumFieldDecorator.DEFAULT_TIMEUNIT, | ||
getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements)); | ||
System.out.println(String.valueOf(AppiumFieldDecorator.DEFAULT_IMPLICITLY_WAIT_TIMEOUT) | ||
+ " " + AppiumFieldDecorator.DEFAULT_TIMEUNIT.toString() + ": Fine"); | ||
|
||
checkTimeDifference(5, TimeUnit.SECONDS, | ||
getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements2)); | ||
System.out.println(String.valueOf(5) | ||
+ " " + TimeUnit.SECONDS.toString() + ": Fine"); | ||
|
||
|
||
timeOutDuration.setTime(15500000, TimeUnit.MICROSECONDS); | ||
checkTimeDifference(15500000, TimeUnit.MICROSECONDS, getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements)); | ||
System.out.println("Change time: " + String.valueOf(15500000) + " " | ||
+ TimeUnit.MICROSECONDS.toString() + ": Fine"); | ||
|
||
checkTimeDifference(5, TimeUnit.SECONDS, | ||
getBenchMark(pageObjectWithCustomizedTimeOuts.stubElements2)); | ||
System.out.println(String.valueOf(5) | ||
+ " " + TimeUnit.SECONDS.toString() + ": Fine"); | ||
|
||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
...les/java/junit/src/test/java/com/saucelabs/appium/driver_service/DriverServiceSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package com.saucelabs.appium.driver_service; | ||
|
||
import io.appium.java_client.MobileElement; | ||
import io.appium.java_client.android.AndroidDriver; | ||
import io.appium.java_client.remote.MobileCapabilityType; | ||
import io.appium.java_client.service.local.AppiumDriverLocalService; | ||
import io.appium.java_client.service.local.AppiumServiceBuilder; | ||
import io.appium.java_client.service.local.flags.GeneralServerFlag; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.openqa.selenium.Platform; | ||
import org.openqa.selenium.remote.DesiredCapabilities; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.util.Properties; | ||
|
||
/** | ||
* Please make sure that: | ||
* - there is an installed node.js and the path to the folder which contains node.js executable is defined at | ||
* PATH environmental variable | ||
* - there is an installed appium.js (via npm) | ||
* - all appium servers are shut down | ||
* - take a look at custom_node_path.properties. If these files don't exist then please | ||
* set your values or set up Appium for desktop | ||
*/ | ||
|
||
/* | ||
This sample shows how to use AppiumServiceBuilder and AppiumDriverLocalService | ||
*/ | ||
public class DriverServiceSample { | ||
|
||
private static Properties properties; | ||
|
||
@BeforeClass | ||
public static void beforeClass() throws Exception{ | ||
File file = new File("src/test/java/com/saucelabs/appium/driver_service/custom_node_path.properties"); | ||
FileInputStream fileInput = new FileInputStream(file); | ||
properties = new Properties(); | ||
properties.load(fileInput); | ||
fileInput.close(); | ||
} | ||
|
||
private static File findCustomNode(){ | ||
Platform current = Platform.getCurrent(); | ||
if (current.is(Platform.WINDOWS)) | ||
return new File(String.valueOf(properties.get("path.to.custom.node.win"))); | ||
|
||
if (current.is(Platform.MAC)) | ||
return new File(String.valueOf(properties.get("path.to.custom.node.macos"))); | ||
|
||
return new File(String.valueOf(properties.get("path.to.custom.node.linux"))); | ||
} | ||
|
||
@Test | ||
public void checkTheAbilityToStartADriverWithTheDefaultServerAndNode(){ | ||
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(MobileCapabilityType.DEVICE_NAME, "Android Emulator"); | ||
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath()); | ||
new AndroidDriver<MobileElement>(capabilities).quit(); | ||
} | ||
|
||
@Test | ||
public void checkTheAbilityToStartADriverWithTheDefaultServerAndNotDefaultNode(){ | ||
System.setProperty(AppiumServiceBuilder.APPIUM_NODE_PROPERTY, findCustomNode().getAbsolutePath()); | ||
|
||
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(MobileCapabilityType.DEVICE_NAME, "Android Emulator"); | ||
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath()); | ||
|
||
try { | ||
new AndroidDriver<MobileElement>(capabilities).quit(); | ||
} | ||
finally { | ||
System.clearProperty(AppiumServiceBuilder.APPIUM_NODE_PROPERTY); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void checkTheAbilityToBuildServiceAndStartADriver(){ | ||
|
||
File classpathRoot = new File(System.getProperty("user.dir")); | ||
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin"); | ||
File app = new File(appDir, "ApiDemos-debug.apk"); | ||
|
||
AppiumServiceBuilder builder = new AppiumServiceBuilder().withAppiumJS(findCustomNode()).withArgument(GeneralServerFlag.APP, | ||
app.getAbsolutePath()).withArgument(GeneralServerFlag.LOG_LEVEL, "info").usingAnyFreePort() /*and so on*/; | ||
|
||
DesiredCapabilities capabilities = new DesiredCapabilities(); | ||
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator"); | ||
|
||
new AndroidDriver<MobileElement>(builder, capabilities).quit(); | ||
new AndroidDriver<MobileElement>(builder.build(), capabilities).quit(); | ||
|
||
} | ||
|
||
@Test | ||
public void checkTheAbilityToUseServiceSeparatelyFromADriver(){ | ||
|
||
File classpathRoot = new File(System.getProperty("user.dir")); | ||
File appDir = new File(classpathRoot, "../../../apps/ApiDemos/bin"); | ||
File app = new File(appDir, "ApiDemos-debug.apk"); | ||
|
||
AppiumServiceBuilder builder = new AppiumServiceBuilder().withAppiumJS(findCustomNode()).withArgument(GeneralServerFlag.APP, | ||
app.getAbsolutePath()).withArgument(GeneralServerFlag.LOG_LEVEL, "info").usingAnyFreePort() /*and so on*/; | ||
AppiumDriverLocalService service = builder.build(); | ||
|
||
DesiredCapabilities capabilities = new DesiredCapabilities(); | ||
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator"); | ||
|
||
service.start(); | ||
try { | ||
new AndroidDriver<MobileElement>(service.getUrl(), capabilities).quit(); | ||
} | ||
finally { | ||
service.stop(); | ||
} | ||
|
||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
.../java/junit/src/test/java/com/saucelabs/appium/driver_service/custom_node_path.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
path.to.custom.node.win=C:/Program Files (x86)/Appium/node_modules/appium/bin/appium.js | ||
path.to.custom.node.macos=/Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js | ||
path.to.custom.node.linux=specify your path on your own |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...unit/src/test/java/com/saucelabs/appium/page_object/PageObjectWithCustomizedTimeOuts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.saucelabs.appium.page_object; | ||
|
||
import io.appium.java_client.MobileElement; | ||
import io.appium.java_client.pagefactory.WithTimeout; | ||
import org.openqa.selenium.support.FindBy; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class PageObjectWithCustomizedTimeOuts { | ||
|
||
/** | ||
* Page Object best practice is to describe interactions with target | ||
* elements by methods. This methods describe business logic of the page/screen. | ||
* Here lazy instantiated elements are public. | ||
* It was done so just for obviousness | ||
*/ | ||
|
||
@FindBy(className = "OneClassWhichDoesNotExist") | ||
public List<MobileElement> stubElements; | ||
|
||
/*Any timeout of the waiting for element/list of elements | ||
can be customized if the general time duration is | ||
not suitable. E.g. the element is rendered for a long time | ||
or the element is used just for instant checkings/assertions | ||
*/ | ||
@WithTimeout(time = 5, unit = TimeUnit.SECONDS) | ||
@FindBy(className = "OneAnotherClassWhichDoesNotExist") | ||
public List<MobileElement> stubElements2; | ||
} |