我必须使用第一个文件包含登录详细信息的文件。第一个文件是loginDetailsClass.java,下面是文件中的代码。
public class loginDetailsClass {
@SuppressWarnings("deprecation")
public static void browserLaunch() {
WebDriver driver;
String baseurl = "https://www.facebook.com/";
System.setProperty("webdriver.chrome.driver","D:\\eclipse-workspace\\chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
options.addArguments("--disable-extensions");
options.addArguments("--disable-infobars");
capabilities.setCapability("chrome.binary","D:\\eclipse-workspace\\chromedriver.exe");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.get(baseurl);
//Login section
try {
Thread.sleep(7000); // 1000 milliseconds is one second.
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
WebElement usnfield = driver.findElement(By.id("email"));
usnfield.sendKeys("alex");
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
WebElement pwdfield = driver.findElement(By.id("pass"));
pwdfield.sendKeys("alex42");
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
WebElement lgn_btn = driver.findElement(By.id("u_0_8"));
lgn_btn.click();
}
}
第二个文件是facebookClass.java,下面是文件中的代码。
public class facebookClass {
static WebDriver driver;
@Test(priority = 0)
public static void main(String[] args) {
loginDetailsClass.browserLaunch(); //function called from loginDetailsClass.java page
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
@Test(priority = 1)
public static void searchfriends() {
//Switching to iFrame for locating elements
driver.switchTo().frame(driver.findElement(By.id("contentFrame")));
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
//Searching a friend using friend name field
WebDriverWait wait = new WebDriverWait (driver, 20);
WebElement friendName = wait.until(ExpectedConditions.elementToBeClickable(By.id("txtFriendName")));
friendName.sendKeys("James");
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
WebElement clck_Search = driver.findElement(By.id("btnSearch"));
clck_Search.click();
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
WebElement select_friend = driver.findElement(By.xpath("//*[@id=\"trFriend_3\"]"));
select_friend.click();
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
//Switching back to main frame from iFrame
driver.switchTo().defaultContent();
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
@Test(priority = 2)
public static void logout() {
//Logout
WebElement logout = driver.findElement(By.xpath("/html/body/form/table/tbody/tr[1]/td/div/div[8]/table/tbody/tr/td[2]"));
logout.click();
try {
Thread.sleep(7000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
driver.close();
}
}
执行facebookClass.java时,正确调用login方法并且登录成功。但是从 public static void searchfriends()方法开始,执行不起作用。在最初的几天,所有方法都成功执行。但是从第二天起它就开始工作了。