appium 怎么启动chrome
发布网友
发布时间:2022-12-22 03:43
我来回答
共1个回答
热心网友
时间:2024-12-02 19:29
package com.dbyl.core;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
public class {
AndroidDriver<WebElement> driver;
/**
* @author Young
* @throws IOException
*/
public void startRecord() throws IOException {
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() throws Exception {
// set up appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "chrome");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("platformVersion", "4.4");
driver = new AndroidDriver<WebElement>(new URL(
"http://127.0.0.1:4723/wd/hub"), capabilities);
startRecord();
}
@Test
public void runChromeWebBrowser() {
driver.get("http://www.baidu.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
System.out.println(driver.getTitle());
snapshot((TakesScreenshot) driver, "before_search.png");
driver.findElementByXPath("//textarea[@id='index-kw']").sendKeys(
"appium");
driver.findElement(By.xpath("//button[@id='index-bn']")).click();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
System.out.println(driver.getTitle());
snapshot((TakesScreenshot) driver, "after_search.png");
Assert.assertTrue(driver.getTitle().contains("appium"));
}
/**
* This Method create for take screenshot
*
* @author Young
* @param drivername
* @param filename
*/
public static void snapshot(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");
}
}
@AfterClass
public void afterTestStopDriver() {
driver.quit();
}
}
这其中,遇到输入框识别的问题,刚开始是使用UIAutomator
发现 web 的element 压根识别不了,最后想到这应该和selenium webdriver一样
所以在识别百度输入框,By xpath
"//textarea[@id='index-kw']"