gpt4 book ai didi

android - 如何使用 appium 在 android 上自动化浏览器?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:39 25 4
gpt4 key购买 nike

我想在android手机上使用appium来自动化浏览器,但是我不知道如何设置能力。

首先,我在我的 Android 设备上的开发者选项中启用了 USB 调试。

其次,adb 运行良好,我可以看到设备 ID。

第三,我从Appium for windows启动了Appium.exe,用JAVA写了一些代码,但我不知道如何在Android浏览器上设置能力。

public class Test {
private WebDriver driver;

@Before
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
//------------I don't know how to set the capability------------//
capabilities.setCapability(CapabilityType.VERSION, "2.3.7");
capabilities.setCapability(CapabilityType.PLATFORM, "WINDOWS");
//--------------------------------------------------------------//
driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}

@Test
public void testcase_001() throws Exception{
driver.get("http://www.google.com");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("lst-ib")));
WebElement keyword = driver.findElement(By.name("lst-ib"));
keyword.sendKeys("appium");
driver.findElement(By.id("btnK")).click();
Thread.sleep(5000);
}

@After
public void tearDown() throws Exception {
driver.quit();
}

public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
private RemoteTouchScreen touch;
public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
super(remoteAddress, desiredCapabilities);
touch = new RemoteTouchScreen(getExecuteMethod());
}
public TouchScreen getTouch() {
return touch;
}
}
}

非常感谢。

最佳答案

使用 Android 驱动程序尝试此代码:

import io.appium.java_client.android.AndroidDriver;

public class Test {
private AndroidDriver;

@Before
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "MOTO G 2");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
capabilities.setCapability("platformVersion", "5.0.2");
capabilities.setCapability("appPackage", "com.android.chrome");
capabilities.setCapability("appActivity","com.google.android.apps.chrome.ChromeTabbedActivity");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}

@Test
public void testcase_001() throws Exception{
driver.get("http://www.google.com");

WebElement keyword = driver.findElementByName("q");
keyword.sendKeys("appium");
driver.findElement(By.id("btnK")).click();
Thread.sleep(5000);
}

@After
public void tearDown() throws Exception {
driver.quit();
}

注意:使用selenium v​​ersion 2.48.2(最新版本)可以使android驱动运行无报错。

关于android - 如何使用 appium 在 android 上自动化浏览器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21977955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com