如何在Android项目中包含Selenium库?

时间:2019-02-24 11:57:03

标签: java android selenium

我尝试为我的android应用程序创建我的第一个自动测试。我找到了一个有用的video,并逐步进行了相同的操作,但是在包含库并重建projeck之后,仍然找不到一些库:

  • 导入org.openqa.selenium。通过;导入
  • org.openqa.selenium。远程 .CapabilityType;导入
  • org.openqa.selenium。远程。DesiredCapabilities;导入
  • org.openqa.selenium。远程 .RemoteWebDriver;

我的代码:

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;



public class FirstTest {

 WebDriver driver;

 @Before
 public void setUp() throws MalformedURLException {
     // Created object of DesiredCapabilities class.
     DesiredCapabilities capabilities = new DesiredCapabilities();

     // Set android deviceName desired capability. Set your device name.
     capabilities.setCapability("deviceName", "XT1562");

     // Set BROWSER_NAME desired capability. It's Android in our case here.
     capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");

     // Set android VERSION desired capability. Set your mobile device's OS version.
     capabilities.setCapability(CapabilityType.VERSION, "6.0.1");

     // Set android platformName desired capability. It's Android in our case here.
     capabilities.setCapability("platformName", "Android");

     // Set android appPackage desired capability. It is
     // com.android.calculator2 for calculator application.
     // Set your application's appPackage if you are using any other app.
     capabilities.setCapability("appPackage", "com.android.calculator2");

     // Set android appActivity desired capability. It is
     // com.android.calculator2.Calculator for calculator application.
     // Set your application's appPackage if you are using any other app.
     capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");

     // Created object of RemoteWebDriver will all set capabilities.
     // Set appium server address and port number in URL string.
     // It will launch calculator app in android device.
     driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
     driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);


 }


 @Test
 public void testFirstCalculator() {


     // Click on DELETE/CLR button to clear result text box before running test.
     driver.findElements(By.xpath("//android.widget.Button")).get(0).click();

     // Click on number 2 button.
     driver.findElement(By.name("7")).click();

     driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
 }

 @After
 public void End() {
     driver.quit();
 }
}

我使用:

  • io.appium Java客户端v7.0。 (link
  • Selenium独立服务器v3.141.59(link

pick from my app

运行我的应用程序后,我有错误列表:


  Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add 
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
Error:1 error; aborting
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: Return code 1 for dex process

我的build.gradle文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您需要更新构建工具。试试

classpath 'com.android.tools.build:gradle:3.0.0'

如果这不起作用,则可能需要弄乱特定于android-module的build.gradle。