Java 学习笔记 (二) Selenium WebDriver Java 弹出框

时间:2021-10-31 10:13:27

下面这段实例实现了以下功能:

1. profile使用用户本地电脑上的 (selenium 3有问题.因为selenium 3把profile复制到一个temp文件夹里,但并不复制回去.所以每次打开仍是一个空的浏览器.问题待解决)

2. 取出多个跳出框的title和内容

3. 验证打开页面的title是否正确

4. 获取页面弹出框中的验证码

 package com.qiujy.testweb_mvn;
import java.io.File;
import java.io.IOException;
import java.util.List; import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait; public class rememberme {
private static final String WebElement = null; /**
* @param args
* @throws InterruptedException
* @throws IOException
*/
public static void main(String[] args) throws InterruptedException, IOException {
System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.19.1-win64\\geckodriver.exe");
ProfilesIni pi = new ProfilesIni();
FirefoxProfile profile = pi.getProfile("default");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options); driver.manage().window().maximize();
driver.get("http://xxxxx/login");
String sysTitle = driver.getTitle();
String expectedTitle="Dva Demo";
if (sysTitle.equals(expectedTitle)) {
System.out.println(sysTitle);
} else {
System.out.println("Wrong Title");
} //点“注册”,打开注册页面
WebElement addButton=driver.findElement(By.xpath("//*[@id=\"root\"]/div/form/div[5]/a"));
addButton.click();
Thread.sleep(1000); //输入注册信息
WebElement userName=driver.findElement(By.xpath("//*[@id=\"nickname\"]"));
userName.sendKeys("abc");
WebElement password=driver.findElement(By.xpath("//*[@id=\"password\"]"));
password.sendKeys("123456");
WebElement cpassword=driver.findElement(By.xpath("//*[@id=\"confirm\"]"));
cpassword.sendKeys("123456");
WebElement mailbox=driver.findElement(By.xpath("//*[@id=\"email\"]"));
mailbox.sendKeys("abc@000.com");
WebElement add=driver.findElement(By.xpath("//*[@id=\"residence\"]"));
add.click();
WebElement add1=driver.findElement(By.xpath("/html/body/div[2]/div/div/div/ul[1]/li[1]"));
add1.click();
System.out.println(add1);
WebElement add2=driver.findElement(By.xpath("/html/body/div[2]/div/div/div/ul[2]/li"));
add2.click();
System.out.println(add2);
WebElement add3=driver.findElement(By.xpath("/html/body/div[2]/div/div/div/ul[3]/li"));
add3.click();
System.out.println(add3);
WebElement cellphone=driver.findElement(By.xpath("//*[@id=\"phone\"]"));
cellphone.sendKeys("123456789"); //获取验证码
WebElement verifycode=driver.findElement(By.className("ant-btn"));
verifycode.click();
WebElement text=driver.findElement(By.className("ant-notification-notice-description"));
String str=text.getText();
String str1=str.replaceAll("请在5min内输入验证码:","");
System.out.println(str1);
WebElement inputcode=driver.findElement(By.xpath("//*[@id=\"captcha\"]"));
inputcode.sendKeys(str1);
WebElement checkbox=driver.findElement(By.xpath("//*[@id=\"root\"]/div/form/div[8]/div/div/label/span[1]/input"));
checkbox.click();
File screenshotFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile, new File("E:\\screenshot\\Register"+System.currentTimeMillis()+".png"));
WebElement register=driver.findElement(By.xpath("//*[@id=\"root\"]/div/form/div[9]/div/div/button"));
register.click(); //取出注册成功的提示字样
WebElement text2=(new WebDriverWait(driver,10)).until(new ExpectedCondition<WebElement>() {
public org.openqa.selenium.WebElement apply(WebDriver driver) {
List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-description"));
if(eles.size() == 2 && eles.get(1).isDisplayed()) {
return eles.get(1);
}
return null;
}
}); System.out.println(text2.getText().toString()); Thread.sleep(1000);
File screenshotFile1=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile1, new File("E:\\screenshot\\Success"+System.currentTimeMillis()+".png")); //用刚刚注册的帐号登录
WebElement uName=driver.findElement(By.xpath("//*[@id=\"userName\"]"));
uName.sendKeys("abc");
WebElement pwd=driver.findElement(By.xpath("//*[@id=\"password\"]"));
pwd.sendKeys("123456");
WebElement Button=driver.findElement(By.xpath("//*[@id=\"root\"]/div/form/div[4]/button"));
Button.click();
File screenshotFile2=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile2, new File("E:\\screenshot\\login"+System.currentTimeMillis()+".png")); //取出登录成功的提示框title
WebElement text3=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
public org.openqa.selenium.WebElement apply(WebDriver driver){
List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-message"));
if(eles.size()==3 && eles.get(2).isDisplayed()) {
return eles.get(2);
}
return null;
}
});
System.out.println(text3.getText().toString()); //取出登录成功的提示字样
WebElement text4=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
public org.openqa.selenium.WebElement apply(WebDriver driver){
List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-description"));
if(eles.size()==3 && eles.get(2).isDisplayed()) {
return eles.get(2);
}
return null;
}
});
System.out.println(text4.getText().toString()); File screenshotFile3=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile3, new File("E:\\screenshot\\LS"+System.currentTimeMillis()+".png")); Thread.sleep(10000); // 重新登录的时候因为已经记忆了密码和帐号信息, 所以不用再次输入, 直接点login就好
driver.get("http://xxxxx/login");
// WebElement uName2=driver.findElement(By.xpath("//*[@id=\"userName\"]"));
// uName2.sendKeys("abc");
// WebElement pwd2=driver.findElement(By.xpath("//*[@id=\"password\"]"));
// pwd2.sendKeys("123456");
File screenshotFile4=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile4, new File("E:\\screenshot\\relogin"+System.currentTimeMillis()+".png"));
WebElement Button2=driver.findElement(By.xpath("//*[@id=\"root\"]/div/form/div[4]/button"));
Button2.click();
WebElement text5=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
public org.openqa.selenium.WebElement apply(WebDriver driver){
List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-description"));
if(eles.size()==2 && eles.get(1).isDisplayed()) {
return eles.get(1);
}
return null;
}
});
System.out.println(text5.getText().toString());
Thread.sleep(1000);
File screenshotFile5=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile5, new File("E:\\screenshot\\reloginpost"+System.currentTimeMillis()+".png")); // 再次打开登录页面,输入帐户密码(因系统已记忆 ,再输入会造成帐户密码信息double, 如帐户变成: abcabc)
driver.get("http://xxxxx/login");
WebElement uName2=driver.findElement(By.xpath("//*[@id=\"userName\"]"));
uName2.sendKeys("abc");
WebElement pwd2=driver.findElement(By.xpath("//*[@id=\"password\"]"));
pwd2.sendKeys("123456");
File screenshotFile6=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile6, new File("E:\\screenshot\\relogin2"+System.currentTimeMillis()+".png"));
WebElement Button3=driver.findElement(By.xpath("//*[@id=\"root\"]/div/form/div[4]/button"));
Button3.click();
Thread.sleep(1000);
// 实际出现了三个提示框 ,前两个显示登录成功, 最后一个显示失败,但是仍然打开了内容页。
//取出第一个提示框title
WebElement text6=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
public org.openqa.selenium.WebElement apply(WebDriver driver){
List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-message"));
if(eles.size()==2 && eles.get(0).isDisplayed()) {
return eles.get(0);
}
return null;
}
});
System.out.println(text6.getText().toString()); //取出第一个提示框内容
WebElement text7=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
public org.openqa.selenium.WebElement apply(WebDriver driver){
List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-description"));
if(eles.size()==2 && eles.get(0).isDisplayed()) {
return eles.get(0);
}
return null;
}
});
System.out.println(text7.getText().toString()); //取出第二个提示框title
WebElement text8=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
public org.openqa.selenium.WebElement apply(WebDriver driver){
List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-message"));
if(eles.size()==2 && eles.get(1).isDisplayed()) {
return eles.get(1);
}
return null;
}
});
System.out.println(text8.getText().toString()); //取出第二个提示框内容
WebElement text9=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
public org.openqa.selenium.WebElement apply(WebDriver driver){
List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-description"));
if(eles.size()==2 && eles.get(1).isDisplayed()) {
return eles.get(1);
}
return null;
}
});
System.out.println(text9.getText().toString()); Thread.sleep(1000);
File screenshotFile7=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile7, new File("E:\\screenshot\\reloginpost"+System.currentTimeMillis()+".png")); driver.quit(); } } /**
*@author: Created by Qian
*@date: 2017年12月27日
*@problem: after login successfully, reopen browser(with cookies), input name/password(which make the data double. e.g. name :abcabc), then click login, window pops up to alert user that login failed, but actually it's still navigated to content page. this can be reproduced no matter manually or by script.
**@answer:
*@action:
*/