【问题标题】:Appium Mobile Web test automation Android for TwitterAppium 移动 Web 测试自动化 Android for Twitter
【发布时间】:2016-12-27 00:53:48
【问题描述】:

我正在为我的 Android 设备在 selenium 服务器上使用 Appium 制作一个测试自动机。自动化程序启动 Twitter 的 web 应用程序进行登录并发布一条推文。但是,我遇到困难的部分是自动机没有使用我的 Chrome 浏览器上已经登录的帐户。为什么每次都要登录?是因为会话刷新吗?有没有办法避免这种情况?

我的代码:

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();

}

非常感谢任何帮助! :)

【问题讨论】:

    标签: android selenium automated-tests appium selendroid


    【解决方案1】:

    Twitter 通过设置客户端token cookies 处理会话。此令牌是通常由您的凭据和浏览器指纹组成的哈希码。由于WebDriver 每次“清除”浏览器时都会启动,当然没有cookies - 服务器无法识别您并要求进行身份验证。

    解决方案:

    • 接受这个。每次登录。它看起来像理智的测试用例。我会推荐这个决定。
    • 保存令牌 cookie _twitter_sess
      Cookie ck = new Cookie("_twitter_sess", "your_authorised_session_id");
      driver.manage().addCookie(ck);
    

    风险: 1) 此会话 cookie 有过期日期。您必须每天早上复制粘贴新的your_authorised_session_id。 2) Twitter 可能有棘手的安全系统,他们检查browser fingerprint

    • 使用您现有的Chrome Profile,这意味着您的浏览器“不清晰”,其中包含您的历史记录和cookie。因此,您必须在运行测试之前手动登录 Twitter,并且它会持续存在。
    ChromeOptions options = new ChromeOptions();
    options.addArguments("user-data-dir=C:/Users/user_name<your_path_to_installed_chrome_in_mob_phone_in_your case>/AppData/Local/Google/Chrome/UserData");
    driver = new ChromeDriver(options);
    

    【讨论】:

    • 谢谢!这实际上是有道理的!我会试试这个
    猜你喜欢
    • 2016-08-23
    • 1970-01-01
    • 2015-04-08
    • 2020-06-30
    • 2014-08-03
    • 2021-10-11
    • 1970-01-01
    • 2021-08-27
    • 2018-09-15
    相关资源
    最近更新 更多