首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Appium Mobile Web测试自动化Android for Twitter

Appium Mobile Web测试自动化Android for Twitter
EN

Stack Overflow用户
提问于 2016-08-19 19:10:42
回答 1查看 810关注 0票数 2

我正在为我的Android设备制作一个在selenium服务器上使用Appium的测试自动化程序。automator启动Twitter的webapp,进行登录并发布Tweet。然而,我遇到的问题是,automator没有使用我的Chrome浏览器上已经登录的帐户。为什么每次我都要登录?是因为会话更新了吗?有什么办法可以避免这种情况吗?

我的代码:

代码语言:javascript
复制
import java.net.MalformedURLException;  
import java.net.URL;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

import org.eclipse.jetty.util.log.Log;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class StartChrome
{
private String email="your_email";
private String password="your_password";
private WebDriver driver=null;
private int flag=0;

@BeforeTest
public void test1() throws MalformedURLException, InterruptedException{

// Create object of  DesiredCapabilities class and specify android platform
DesiredCapabilities capabilities=DesiredCapabilities.android();


// set the capability to execute test in chrome browser
 capabilities.setCapability(MobileCapabilityType.BROWSER_NAME,BrowserType.CHROME);

// set the capability to execute our test in Android Platform
   capabilities.setCapability(MobileCapabilityType.PLATFORM,Platform.ANDROID);

// we need to define platform name
  capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");

// Set the device name as well (you can give any name)
 capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"my phone");

 // set the android version as well 
   capabilities.setCapability(MobileCapabilityType.VERSION,"6.0.1");

 // Create object of URL class and specify the appium server address
 URL url= new URL("http://127.0.0.1:4727/wd/hub");

// Create object of  AndroidDriver class and pass the url and capability that we created
 WebDriver driver = new AndroidDriver(url, capabilities);

// Open url
  driver.get("http://www.twitter.com");

 // print the title
  System.out.println("Title "+driver.getTitle());
    try{
  driver.findElement(By.id("react-root"));      
    }catch(Exception e){
        driver.findElement(By.linkText("Log in")).click();
        Thread.sleep(3000);
        driver.findElement(By.name("session[username_or_email]")).sendKeys(email);
        driver.findElement(By.name("session[password]")).sendKeys(password);
        driver.findElement(By.id("signupbutton")).click();
        Thread.sleep(5000);
        driver.findElement(By.cssSelector("a[href*='/compose/tweet']")).click();    
        flag=1;
    }
    finally{
        if(flag==0){
            driver.findElement(By.cssSelector("a[href*='/compose/tweet']")).click();    
            }
    }
    driver.findElement(By.cssSelector("textarea[aria-label*='Tweet text']")).sendKeys("Test");;
    //driver.findElement(By.linkText("Tweet")).click();
    Thread.sleep(2000);
    driver.findElement(By.cssSelector("button[data-testid*='Button']")).click();


Thread.sleep(2000);
driver.quit();

}

如有任何帮助,将不胜感激!)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-19 20:46:38

Twitter会话通过设置客户端令牌曲奇来处理。此标记通常是由凭据和浏览器指纹组成的哈希码。由于WebDriver每次启动“清除”浏览器,没有cookie,服务器当然不会识别您,并要求身份验证。

解决方案:

  • 接受这个吧。每次登录。看上去是个正常的测试用例。我会推荐这个决定。
  • 保存令牌cookies _twitter_sess

Cookie ck =新Cookie("_twitter_sess","your_authorised_session_id");driver.manage().addCookie(ck);

风险: 1)此会话cookie有过期日期。你每天早上都要复制新的your_authorised_session_id。2) Twitter有可能有棘手的安全系统,他们检查浏览器指纹

  • 使用您现有的Chrome Profile,它意味着您的浏览器‘非清除’,其中包含您的历史记录,您的cookie。因此,在运行测试之前,您必须手动登录twitter,它将持续存在。

options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/UserData");

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39046200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档