Android设备上的WebDriver

时间:2018-01-22 21:53:32

标签: java android selenium webdriver selendroid

我的目标是能够在没有javascript注入的情况下在我的Android设备上启动浏览器(因此没有webviews + js)并自动化它(抓取,发布表单,更改页面,单击按钮等)。

我宁愿不使用JS,因为我希望我的自动化完全无法检测到"来自网站(验证码是一个不同的故事,而不是我目前的问题)。

我想通过Selendroid使用WebDriver API,但我所有的努力都失败了。 这是我写的代码:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SelendroidCapabilities caps = SelendroidCapabilities.empty();
        caps.setJavascriptEnabled(true);
        caps.setAut("com.myapp.myapp");
        caps.setLaunchActivity("com.myapp.myapp.WebViewActivity"); // activity containing a webview only

        WebDriver driver = null;
        try {
            driver = new SelendroidDriver(caps); // !! it fails here !!
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        driver.get("https://www.example.com");

        WebElement inputField = driver.findElement(By.id("my_text_field"));
        inputField.sendKeys("Selendroid");
        driver.quit();
    }
}

这是我的build.gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.myapp.myapp"
        minSdkVersion 23
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'README.md'
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

    compile files('libs/selendroid-client-0.17.0.jar')
    compile files('libs/selendroid-standalone-0.17.0-with-dependencies.jar')
}

这是错误:

Process: com.myapp.myapp, PID: 25868
java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/message/BasicLineFormatter; in class Lorg/apache/http/message/BasicLineFormatter; or its superclasses (declaration of 'org.apache.http.message.BasicLineFormatter' appears in /system/framework/org.apache.http.legacy.boot.jar)
   at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:52)
   at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:56)
   at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:46)
   at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:72)
   at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:84)
   at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConnectionFactory.java:59)
   at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:493)
   at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:149)
   at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:138)
   at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:114)
   at org.openqa.selenium.remote.internal.HttpClientFactory.getClientConnectionManager(HttpClientFactory.java:74)
   at org.openqa.selenium.remote.internal.HttpClientFactory.<init>(HttpClientFactory.java:57)
   at org.openqa.selenium.remote.internal.HttpClientFactory.<init>(HttpClientFactory.java:60)
   at org.openqa.selenium.remote.internal.ApacheHttpClient$Factory.getDefaultHttpClientFactory(ApacheHttpClient.java:251)
   at org.openqa.selenium.remote.internal.ApacheHttpClient$Factory.<init>(ApacheHttpClient.java:228)
   at org.openqa.selenium.remote.HttpCommandExecutor.getDefaultClientFactory(HttpCommandExecutor.java:96)
   at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:70)
   at io.selendroid.client.SelendroidCommandExecutor.<init>(SelendroidCommandExecutor.java:77)
   at io.selendroid.client.SelendroidDriver.<init>(SelendroidDriver.java:84)
   at com.myapp.myapp.MainActivity$1.run(MainActivity.java:45)

P.S。 我已经尝试过这个解决方案Getting NoSuchFieldError INSTANCE org/apache/http/message/BasicHeaderValueParser

建议?

提前致谢

0 个答案:

没有答案