2016-08-19 4 views
2

Ich machte einen Testautomator mit Appium auf einem Selenserver für mein Android-Gerät. Der Automator startet die Twitter-Webapp, loggt sich ein und postet einen Tweet. Der Teil, mit dem ich Schwierigkeiten habe, ist jedoch, dass der Automator nicht das bereits eingeloggte Konto in meinem Chrome-Browser verwendet. Warum muss ich mich jedes Mal anmelden? Liegt es daran, dass die Sitzung aktualisiert wird? Gibt es eine Möglichkeit, dies zu vermeiden?Appium Mobile Web Testautomatisierung Android für Twitter

Mein Code:

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

} 

Jede Hilfe ist sehr zu schätzen! :)

Antwort

2

Twitter Sitzungshandles durch Einstellung der Clientseite token cookies. Bei diesem Token handelt es sich um einen Hashcode, der häufig aus Ihren Anmeldeinformationen und dem Fingerabdruck Ihres Browsers besteht. Seit WebDriver startet jedes Mal "klar" Browser, ohne Cookies natürlich - Server erkennt Sie nicht und fragen nach Authentifizierung.

Lösungen:

  • dies einfach akzeptieren. Melde dich jedes Mal an. Es sieht wie ein vernünftiger Testfall aus. Ich würde diese Entscheidung empfehlen.
  • Token Cookies speichern _twitter_sess.
Cookie ck = new Cookie("_twitter_sess", "your_authorised_session_id"); 
    driver.manage().addCookie(ck); 

Risiken: 1) Diese Session-Cookie hat das Ablaufdatum. Sie müssen jeden Morgen neue your_authorised_session_id kopieren. 2) Es ist möglich, dass Twitter heikel Sicherheitssystem hat und sie überprüfen browser fingerprint

  • Verwenden Sie Ihre Chrome Profile bestehenden, es bedeutet, dass Ihr Browser ‚nicht klar ein‘, die Ihre Geschichte enthält, Ihre Cookies. Sie müssen sich also vor dem Ausführen von Tests manuell bei twitter anmelden und es bleibt bestehen.
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); 
+0

Dank! Das macht Sinn! Ich werde es versuchen –

Verwandte Themen