我正在为我的Android设备制作一个在selenium服务器上使用Appium的测试自动化程序。automator启动Twitter的webapp,进行登录并发布Tweet。然而,我遇到的问题是,automator没有使用我的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();
}如有任何帮助,将不胜感激!)
发布于 2016-08-19 20:46:38
Twitter会话通过设置客户端令牌曲奇来处理。此标记通常是由凭据和浏览器指纹组成的哈希码。由于WebDriver每次启动“清除”浏览器,没有cookie,服务器当然不会识别您,并要求身份验证。
解决方案:
_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");
https://stackoverflow.com/questions/39046200
复制相似问题