Created
September 6, 2018 00:23
-
-
Save nhatthuyld/d3179d9cc93a3b3e0fb08396b27c15fd to your computer and use it in GitHub Desktop.
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
package com.wizeline; | |
import java.io.IOException; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
import org.junit.AfterClass; | |
import org.junit.Assert; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.Dimension; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.remote.DesiredCapabilities; | |
import org.openqa.selenium.remote.RemoteWebDriver; | |
import org.openqa.selenium.support.ui.Sleeper; | |
import io.appium.java_client.AppiumDriver; | |
import io.appium.java_client.MobileElement; | |
import io.appium.java_client.android.AndroidDriver; | |
public class Login { | |
static AndroidDriver<MobileElement> driver; | |
@BeforeClass | |
public static void setUp() throws MalformedURLException { | |
// Set up desired capabilities and pass the Android app-activity and app-package | |
// to Appium | |
DesiredCapabilities capabilities = new DesiredCapabilities(); | |
capabilities.setCapability("BROWSER_NAME", "Android"); | |
// capabilities.setCapability("VERSION", "4.4.2"); | |
capabilities.setCapability("deviceName", "Nexus_5X_API_28"); | |
capabilities.setCapability("automationName", "UiAutomator2"); | |
capabilities.setCapability("platformName", "Android"); | |
capabilities.setCapability("app", "C:\\Users\\DucNGUYEN\\Downloads\\comsumer.apk"); | |
capabilities.setCapability("appPackage", "com.synova.savyu"); | |
// This package name of your app (you can get it from apk info app) | |
capabilities.setCapability("appActivity", "com.synova.savyu.MainActivity"); // This is Launcher | |
// activity of | |
// your app (you can get it | |
// from apk info app) | |
// Create RemoteWebDriver instance and connect to the Appium server | |
// It will launch the Calculator App in Android Device using the configurations | |
// specified in Desired Capabilities | |
driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), capabilities); | |
} | |
//@Test | |
public void signInSucessful() throws Exception { | |
SleepIn(3); | |
clickByXpath("//android.widget.TextView[@text='SKIP']"); | |
sendKeyBoard("//android.widget.EditText[@index ='5']", "nhatthuy@gmail.com"); | |
clickByXpath("//android.widget.TextView[@text='SIGN IN']"); | |
//input pass | |
List<MobileElement> elements = driver.findElementsByXPath("//android.widget.EditText"); | |
// for(MobileElement el: elements) { | |
// System.out.println("ddddd:"+el.getId() + el.getTagName() + el.getText()); | |
// } | |
MobileElement pwdField = elements.get(1) ; | |
pwdField.sendKeys("123456789"); | |
clickByXpath("//android.widget.TextView[@text='SIGN IN']"); | |
} | |
@Test | |
public void signInFailWithInvalidEmail() throws Exception { | |
SleepIn(3); | |
clickByXpath("//android.widget.TextView[@text = 'SKIP']"); | |
//input email lack of @ | |
sendKeyBoard("//android.widget.EditText[@index ='5']", "nhatthuygmail.com"); | |
clickByXpath("//android.widget.TextView[@text = 'SIGN IN']"); | |
findByXpath("//android.widget.TextView[@text = 'Email is not valid']"); | |
} | |
@Test | |
public void signUpFailWithInvalidInformation() throws Exception { | |
SleepIn(3); | |
clickByXpath("//android.widget.TextView[@text = 'SKIP']"); | |
//input email lack of @ | |
sendKeyBoard("//android.widget.EditText[@index ='5']", "nhatthuygmail.com"); | |
clickByXpath("//android.widget.TextView[@text = 'SIGN IN']"); | |
findByXpath("//android.widget.TextView[@text = 'Email is not valid']"); | |
} | |
@AfterClass | |
public static void teardown() throws InterruptedException { | |
// close the app | |
Thread.sleep(5000); | |
driver.quit(); | |
} | |
private void SleepIn(float second) throws InterruptedException { | |
int millisenconds = (int) second * 1000; | |
Thread.sleep(millisenconds); | |
} | |
public MobileElement findByXpath(String xpath) throws InterruptedException { | |
MobileElement skipBtn = driver.findElementByXPath(xpath); | |
return skipBtn; | |
} | |
public void clickByXpath(String xpath) throws InterruptedException { | |
MobileElement skipBtn = driver.findElementByXPath(xpath); | |
skipBtn.click(); | |
SleepIn(4); | |
} | |
public void sendKeyBoard(String xpath, String string) throws InterruptedException { | |
MobileElement skipBtn = driver.findElementByXPath(xpath); | |
skipBtn.sendKeys(string); | |
SleepIn(4); | |
} | |
public void scroll() throws IOException { | |
Dimension dimensions = driver.manage().window().getSize(); | |
System.out.println("Size of screen= " + dimensions); | |
int Startpoint = (int) (dimensions.getHeight() * 0.5); | |
System.out.println("Size of scrollStart= " + Startpoint); | |
int scrollEnd = (int) (dimensions.getHeight() * 0.2); | |
System.out.println("Size of cscrollEnd= " + scrollEnd); | |
driver.swipe(0, Startpoint, 0, scrollEnd, 1000); | |
} | |
public MobileElement findElementHasText(List<MobileElement> elems, String text) { | |
for (MobileElement el : elems) { | |
System.out.println("ddddd:el:" + el.getText()); | |
} | |
for (MobileElement el : elems) { | |
if (el.getText().equals(text)) { | |
return el; | |
} | |
} | |
return null; | |
} | |
} |
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
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.wizeline</groupId> | |
<artifactId>simple-appium</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<dependencies> | |
<!-- https://mvnrepository.com/artifact/junit/junit --> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.12</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>io.appium</groupId> | |
<artifactId>java-client</artifactId> | |
<version>4.1.2</version> | |
</dependency> | |
</dependencies> | |
</project> |
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
package com.wizeline; | |
import java.io.IOException; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
import org.apache.http.util.Asserts; | |
import org.junit.AfterClass; | |
import org.junit.Assert; | |
import org.junit.BeforeClass; | |
import org.junit.FixMethodOrder; | |
import org.junit.Test; | |
import org.junit.runners.MethodSorters; | |
import org.openqa.selenium.Alert; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.Dimension; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.remote.DesiredCapabilities; | |
import org.openqa.selenium.remote.RemoteWebDriver; | |
import org.openqa.selenium.support.ui.Sleeper; | |
import io.appium.java_client.AppiumDriver; | |
import io.appium.java_client.MobileElement; | |
import io.appium.java_client.android.AndroidDriver; | |
@FixMethodOrder(MethodSorters.JVM) | |
public class SignUp { | |
static AndroidDriver<MobileElement> driver; | |
@BeforeClass | |
public static void setUp() throws MalformedURLException { | |
// Set up desired capabilities and pass the Android app-activity and app-package | |
// to Appium | |
DesiredCapabilities capabilities = new DesiredCapabilities(); | |
capabilities.setCapability("BROWSER_NAME", "Android"); | |
// capabilities.setCapability("VERSION", "4.4.2"); | |
capabilities.setCapability("deviceName", "Nexus_5X_API_28"); | |
capabilities.setCapability("automationName", "UiAutomator2"); | |
capabilities.setCapability("platformName", "Android"); | |
capabilities.setCapability("app", "C:\\Users\\DucNGUYEN\\Downloads\\comsumer.apk"); | |
capabilities.setCapability("appPackage", "com.synova.savyu"); | |
// This package name of your app (you can get it from apk info app) | |
capabilities.setCapability("appActivity", "com.synova.savyu.MainActivity"); // This is Launcher | |
// activity of | |
// your app (you can get it | |
// from apk info app) | |
// Create RemoteWebDriver instance and connect to the Appium server | |
// It will launch the Calculator App in Android Device using the configurations | |
// specified in Desired Capabilities | |
driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), capabilities); | |
} | |
@Test | |
public void signUpFailWithInvalidEmail() throws Exception { | |
driver.resetApp(); | |
SleepIn(3); | |
clickByXpath("//android.widget.TextView[@text='SKIP']"); | |
clickByXpath("//android.widget.TextView[@text = 'Sign Up.']"); | |
clickByXpath("//android.widget.TextView[@text = 'REGISTER']"); | |
//sign up with email lack of @ | |
sendKeyBoard("//android.widget.EditText[@index ='7']", "nhatthuygmail.com"); | |
scroll(); | |
clickByXpath("//android.widget.TextView[@text = 'SUBMIT']"); | |
findByXpath("//android.widget.TextView[@text = 'Email is not valid']"); | |
} | |
@Test | |
public void signUpFailWithInvalidPhoneNumber() throws Exception { | |
driver.resetApp(); | |
SleepIn(3); | |
clickByXpath("//android.widget.TextView[@text='SKIP']"); | |
clickByXpath("//android.widget.TextView[@text = 'Sign Up.']"); | |
clickByXpath("//android.widget.TextView[@text = 'REGISTER']"); | |
sendKeyBoard("//android.widget.EditText[@index ='7']", "nhatthuy@gmail.com"); | |
//input invalid phone number | |
List<MobileElement> elements = driver.findElementsByXPath("//android.widget.EditText"); | |
printElements(elements); | |
MobileElement phoneElement = elements.get(4); | |
phoneElement.clear(); | |
phoneElement.sendKeys("6666"); | |
scroll(); | |
clickByXpath("//android.widget.TextView[@text = 'SUBMIT']"); | |
findByXpath("//android.widget.TextView[@text = 'Phone number is not valid']"); | |
} | |
@Test | |
public void signUpSucessful() throws Exception { | |
driver.resetApp(); | |
SleepIn(3); | |
clickByXpath("//android.widget.TextView[@text='SKIP']"); | |
clickByXpath("//android.widget.TextView[@text = 'Sign Up.']"); | |
clickByXpath("//android.widget.TextView[@text = 'REGISTER']"); | |
sendKeyBoard("//android.widget.EditText[@index ='1']", "Nhat Thuy"); | |
sendKeyBoard("//android.widget.EditText[@index ='4']", "Tran Nguyen"); | |
sendKeyBoard("//android.widget.EditText[@index ='7']", "nhatthuy@gmail.com"); | |
scroll(); | |
clickByXpath("//android.widget.TextView[@text = 'SUBMIT']"); | |
scroll(); | |
clickByXpath("//android.widget.TextView[@text = 'SUBMIT']"); | |
//input pass match with confirm pass | |
List<MobileElement> elements = driver.findElementsByXPath("//android.widget.EditText"); | |
MobileElement phoneElement = elements.get(1); | |
phoneElement.clear(); | |
phoneElement.sendKeys("1234"); | |
clickByXpath("//android.widget.TextView[@text = 'SUBMIT']"); | |
//send code sucessful | |
sendKeyBoard("//android.widget.EditText[@index ='2']", "1"); | |
sendKeyBoard("//android.widget.EditText[@index ='3']", "2"); | |
sendKeyBoard("//android.widget.EditText[@index ='4']", "3"); | |
sendKeyBoard("//android.widget.EditText[@index ='5']", "4"); | |
clickByXpath("//android.widget.TextView[@text = 'SUBMIT']"); | |
} | |
@Test | |
public void signUpFailWithPassDoesNotMatchConfirmPass() throws Exception { | |
driver.resetApp(); | |
SleepIn(3); | |
clickByXpath("//android.widget.TextView[@text='SKIP']"); | |
clickByXpath("//android.widget.TextView[@text = 'Sign Up.']"); | |
clickByXpath("//android.widget.TextView[@text = 'REGISTER']"); | |
sendKeyBoard("//android.widget.EditText[@index ='1']", "Nhat Thuy"); | |
sendKeyBoard("//android.widget.EditText[@index ='4']", "Tran Nguyen"); | |
sendKeyBoard("//android.widget.EditText[@index ='7']", "nhatthuy@gmail.com"); | |
scroll(); | |
clickByXpath("//android.widget.TextView[@text = 'SUBMIT']"); | |
scroll(); | |
clickByXpath("//android.widget.TextView[@text = 'SUBMIT']"); | |
//input confirm password doesn't match with pass | |
List<MobileElement> elements = driver.findElementsByXPath("//android.widget.EditText"); | |
MobileElement phoneElement = elements.get(1); | |
phoneElement.clear(); | |
phoneElement.sendKeys("123456789"); | |
clickByXpath("//android.widget.TextView[@text = 'SUBMIT']"); | |
findByXpath("//android.widget.TextView[@text = 'Your password don’t match, please try again']"); | |
} | |
@Test | |
public void signUpFailWithSendCodeFail() throws Exception { | |
driver.resetApp(); | |
SleepIn(3); | |
clickByXpath("//android.widget.TextView[@text='SKIP']"); | |
clickByXpath("//android.widget.TextView[@text = 'Sign Up.']"); | |
clickByXpath("//android.widget.TextView[@text = 'REGISTER']"); | |
sendKeyBoard("//android.widget.EditText[@index ='1']", "Nhat Thuy"); | |
sendKeyBoard("//android.widget.EditText[@index ='4']", "Tran Nguyen"); | |
sendKeyBoard("//android.widget.EditText[@index ='7']", "nhatthuy@gmail.com"); | |
scroll(); | |
clickByXpath("//android.widget.TextView[@text = 'SUBMIT']"); | |
scroll(); | |
clickByXpath("//android.widget.TextView[@text = 'SUBMIT']"); | |
//input pass match with confirm pass | |
clickByXpath("//android.widget.TextView[@text = 'SUBMIT']"); | |
//send code fail | |
sendKeyBoard("//android.widget.EditText[@index ='2']", "0"); | |
sendKeyBoard("//android.widget.EditText[@index ='3']", "0"); | |
sendKeyBoard("//android.widget.EditText[@index ='4']", "k"); | |
sendKeyBoard("//android.widget.EditText[@index ='5']", "j"); | |
clickByXpath("//android.widget.TextView[@text = 'SUBMIT']"); | |
findByXpath("//android.widget.TextView[@text = 'Fail!!!']"); | |
Alert alert = driver.switchTo().alert(); | |
alert.accept(); | |
} | |
//@AfterClass | |
public static void teardown() throws InterruptedException { | |
// close the app | |
Thread.sleep(5000); | |
driver.quit(); | |
} | |
private void SleepIn(float second) throws InterruptedException { | |
int millisenconds = (int) second * 1000; | |
Thread.sleep(millisenconds); | |
} | |
public MobileElement findByXpath(String xpath) throws InterruptedException { | |
MobileElement skipBtn = driver.findElementByXPath(xpath); | |
return skipBtn; | |
} | |
public void clickByXpath(String xpath) throws InterruptedException { | |
MobileElement skipBtn = driver.findElementByXPath(xpath); | |
skipBtn.click(); | |
SleepIn(4); | |
} | |
public void sendKeyBoard(String xpath, String string) throws InterruptedException { | |
MobileElement skipBtn = driver.findElementByXPath(xpath); | |
skipBtn.sendKeys(string); | |
SleepIn(4); | |
} | |
public void scroll() throws IOException { | |
Dimension dimensions = driver.manage().window().getSize(); | |
//System.out.println("Size of screen= " + dimensions); | |
int Startpoint = (int) (dimensions.getHeight() * 0.5); | |
//System.out.println("Size of scrollStart= " + Startpoint); | |
int scrollEnd = (int) (dimensions.getHeight() * 0.2); | |
//System.out.println("Size of cscrollEnd= " + scrollEnd); | |
driver.swipe(0, Startpoint, 0, scrollEnd, 1000); | |
} | |
public MobileElement findElementHasText(List<MobileElement> elems, String text) { | |
for (MobileElement el : elems) { | |
//System.out.println("ddddd:el:" + el.getText()); | |
} | |
for (MobileElement el : elems) { | |
if (el.getText().equals(text)) { | |
return el; | |
} | |
} | |
return null; | |
} | |
public void printElements(List<MobileElement> elems) { | |
for(MobileElement el: elems) { | |
System.out.println("::::el:"+el.getId()+":"+el.getText()+":"+el.getTagName()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment