2
我正在使用Appium在我的Android設備的硒服務器上製作一個測試Automator。 automator啓動Twitter的web應用程序進行登錄併發布Tweet。但是,我遇到困難的部分是,自動工具不會使用我的Chrome瀏覽器中已經登錄的帳戶。爲什麼每次都必須登錄?是否因爲會話刷新?有沒有辦法避免這種情況?Appium Mobile Web測試自動化Android for Twitter
我的代碼:
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();
}
任何幫助,非常感謝! :)
謝謝!這實際上是有道理的!我會試試這個 –