packagecom.dbyl.core;importorg.apache.commons.io.FileUtils;importorg.openqa.selenium.By;importorg.openqa.selenium.NoSuchElementException;importorg.openqa.selenium.OutputType;importorg.openqa.selenium.TakesScreenshot;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.remote.CapabilityType;importorg.openqa.selenium.remote.DesiredCapabilities;importorg.testng.Assert;importorg.testng.annotations.AfterClass;importorg.testng.annotations.BeforeClass;importorg.testng.annotations.Test;importio.appium.java_client.android.AndroidDriver;importjava.io.File;importjava.io.IOException;importjava.net.URL;importjava.util.List;importjava.util.concurrent.TimeUnit;public classzhiHu {privateAndroidDriver driver;private boolean isInstall = false;/***@authorYoung
*@throwsIOException*/
public void startRecord() throwsIOException {
Runtime rt=Runtime.getRuntime();//this code for record the screen of your device
rt.exec("cmd.exe /C adb shell screenrecord /sdcard/runCase.mp4");
}
@BeforeClass(alwaysRun= true)public void setUp() throwsException {//set up appium
DesiredCapabilities capabilities= newDesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME,"");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("platformVersion", "4.4");//if no need install don't add this
if(isInstall) {
File classpathRoot= new File(System.getProperty("user.dir"));
File appDir= new File(classpathRoot, "apps");
File app= new File(appDir, "zhihu.apk");
capabilities.setCapability("app", app.getAbsolutePath());
}
capabilities.setCapability("appPackage", "com.zhihu.android");//support Chinese
capabilities.setCapability("unicodeKeyboard", "True");
capabilities.setCapability("resetKeyboard", "True");//no need sign
capabilities.setCapability("noSign", "True");
capabilities.setCapability("appActivity", ".ui.activity.GuideActivity");
driver= new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
capabilities);
startRecord();
}public voidlogin() {
WebElement loginButton;if (isLoginPresent(driver, 60)) {
loginButton=driver.findElement(By
.id("com.zhihu.android:id/login"));
loginButton.click();
}else{
Assert.assertTrue(false);
}//wait for 20s
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);//find login userName and password editText
List textFieldsList =driver
.findElementsByClassName("android.widget.EditText");
textFieldsList.get(0).sendKeys("seleniumcookies@126.com");
textFieldsList.get(1).sendKeys("cookies123");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);//find ok button byName
driver.findElementById("android:id/button1").click();
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);//find keyword 首页 and verify it is display
Assert.assertTrue(driver.findElement(By.name("首页")).isDisplayed());
}public boolean isLoginPresent(AndroidDriver driver, inttimeout) {boolean isPresent = newAndroidDriverWait(driver, timeout).until(new ExpectedCondition() {publicWebElement apply(AndroidDriver d) {returnd.findElement(By
.id("com.zhihu.android:id/login"));
}
}).isDisplayed();returnisPresent;
}
@Test(groups= "swipeTest", priority = 1)public voidswipe() {//find login button
if(isInstall) {
login();
}
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);//find keyword 首页 and verify it is display
Assert.assertTrue(driver.findElement(By.name("首页")).isDisplayed());
snapshot((TakesScreenshot) driver,"zhihu_before_swipe.png");
swipeToUp(driver,500);
snapshot((TakesScreenshot) driver,"zhihu_after_swipe.png");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
swipeToDown(driver,1000);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
List titles =driver
.findElementsById("com.zhihu.android:id/title");
titles.get(0).click();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);//swipe to right
swipeToRight(driver, 100);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);//find keyword 首页 and verify it is display
Assert.assertTrue(driver.findElement(By.name("首页")).isDisplayed());
}/*** This Method for swipe up
*
*@authorYoung
*@paramdriver
*@paramduring*/
public void swipeToUp(AndroidDriver driver, intduring) {int width =driver.manage().window().getSize().width;int height =driver.manage().window().getSize().height;
driver.swipe(width/ 2, height * 3 / 4, width / 2, height / 4, during);//wait for page loading
}/*** This Method for swipe down
*
*@authorYoung
*@paramdriver
*@paramduring*/
public void swipeToDown(AndroidDriver driver, intduring) {int width =driver.manage().window().getSize().width;int height =driver.manage().window().getSize().height;
driver.swipe(width/ 2, height / 4, width / 2, height * 3 / 4, during);//wait for page loading
}/*** This Method for swipe Left
*
*@authorYoung
*@paramdriver
*@paramduring*/
public void swipeToLeft(AndroidDriver driver, intduring) {int width =driver.manage().window().getSize().width;int height =driver.manage().window().getSize().height;
driver.swipe(width* 3 / 4, height / 2, width / 4, height / 2, during);//wait for page loading
}/*** This Method for swipe Right
*
*@authorYoung
*@paramdriver
*@paramduring*/
public void swipeToRight(AndroidDriver driver, intduring) {int width =driver.manage().window().getSize().width;int height =driver.manage().window().getSize().height;
driver.swipe(width/ 4, height / 2, width * 3 / 4, height / 2, during);//wait for page loading
}
@Test(groups= { "profileSetting" }, priority = 2)public voidprofileSetting() {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);//find keyword 首页 and verify it is display
Assert.assertTrue(driver.findElement(By.name("首页")).isDisplayed());
driver.swipe(100, 400, 100, 200, 500);
WebElement myButton=driver.findElement(By
.className("android.widget.ImageButton"));
myButton.click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.swipe(700, 500, 100, 500, 10);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
List textViews =driver
.findElementsByClassName("android.widget.TextView");
textViews.get(0).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElementById("com.zhihu.android:id/name").click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);//wait for 60s if WebElemnt show up less than 60s , then return , until//60s
By by= new By.ById("com.zhihu.android:id/showcase_close");
snapshot((TakesScreenshot) driver,"zhihu_showClose.png");if (isElementPresent(by, 30)) {
driver.findElement(by).click();
}
Assert.assertTrue(driver
.findElementsByClassName("android.widget.TextView").get(0)
.getText().contains("selenium"));
driver.findElementById("com.zhihu.android:id/menu_people_edit").click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement intro=driver
.findElementById("com.zhihu.android:id/introduction");
intro.click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement content=driver
.findElementById("com.zhihu.android:id/content");
String text= content.getAttribute("text");
content.click();
clearText(text);
content.sendKeys("Appium Test. Create By Young");
driver.findElementById("com.zhihu.android:id/menu_question_done")
.click();
WebElement explanation=driver
.findElementById("com.zhihu.android:id/explanation");
explanation.click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
content= driver.findElementById("com.zhihu.android:id/content");
text= content.getAttribute("text");
content.click();
clearText(text);
content.sendKeys("Appium Test. Create By Young. This is an appium type hahahahah");
driver.findElementById("com.zhihu.android:id/menu_question_done")
.click();
snapshot((TakesScreenshot) driver,"zhihu.png");
}/*** This method for delete text in textView
*
*@authorYoung
*@paramtext*/
public voidclearText(String text) {
driver.sendKeyEvent(123);for (int i = 0; i < text.length(); i++) {
driver.sendKeyEvent(67);
}
}
@AfterClass(alwaysRun= true)public void tearDown() throwsException {
driver.quit();
}/*** This Method create for take screenshot
*
*@authorYoung
*@paramdrivername
*@paramfilename*/
public static voidsnapshot(TakesScreenshot drivername, String filename) {//this method will take screen shot ,require two parameters ,one is//driver name, another is file name
String currentPath= System.getProperty("user.dir"); //get current work//folder
File scrFile =drivername.getScreenshotAs(OutputType.FILE);//Now you can do whatever you need to do with it, for example copy//somewhere
try{
System.out.println("save snapshot path is:" + currentPath + "/"
+filename);
FileUtils
.copyFile(scrFile,new File(currentPath + "\\" +filename));
}catch(IOException e) {
System.out.println("Can't save screenshot");
e.printStackTrace();
}finally{
System.out.println("screen shot finished, it's in " +currentPath+ " folder");
}
}/***
*@paramby
*@paramtimeOut
*@return
*/
public boolean isElementPresent(final By by, inttimeOut) {try{newAndroidDriverWait(driver, timeOut)
.until(new ExpectedCondition() {publicWebElement apply(AndroidDriver d) {returnd.findElement(by);
}
});return true;
}catch(Exception e) {return false;
}
}
}