Java 学习笔记 (二) Selenium WebDriver Java 弹出框
下面这段实例实现了以下功能:
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:
*/
Java 学习笔记 (二) Selenium WebDriver Java 弹出框的更多相关文章
- Java学习笔记二十五:Java面向对象的三大特性之多态
Java面向对象的三大特性之多态 一:什么是多态: 多态是同一个行为具有多个不同表现形式或形态的能力. 多态就是同一个接口,使用不同的实例而执行不同操作. 多态性是对象多种表现形式的体现. 现实中,比 ...
- Java学习笔记二十八:Java中的接口
Java中的接口 一:Java的接口: 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承 ...
- Java学习笔记二十四:Java中的Object类
Java中的Object类 一:什么是Object类: Object类是所有类的父类,相当于所有类的老祖宗,如果一个类没有使用extends关键字明确标识继承另外一个类,那么这个类默认继承Object ...
- Java学习笔记二十六:Java多态中的引用类型转换
Java多态中的引用类型转换 引用类型转换: 1.向上类型转换(隐式/自动类型转换),是小类型到大类型的转换: 2.向下类型转换(强制类型转换),是大类型到小类型的转换: 3.instanceof运算 ...
- java学习笔记(1)java的基础介绍 、JDK下载、配置环境变量、运行java程序
java工程师是开发软件的 什么是软件呢? 计算机包括两部分: 硬件: 鼠标.键盘.显示器.主机箱内部的cpu.内存条.硬盘等 软件: 软件包括:系统软件和应用软件 系统软件:直接和硬件交互的软件:w ...
- Selenium入门10 弹出框的处理 switch_to.alert
三种弹出框alert(一个按钮),confirm(两个确认,取消),prompt(两个按钮+输入框). 切换到弹框: switch_to_alert() 新版的selenium用: brows ...
- Java学习笔记二十七:Java中的抽象类
Java中的抽象类 一:Java抽象类: 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就 ...
- Java学习笔记二--API课堂记录
JavaSE课堂记录(二) 第一节课 方法的重载:方法名相同,参数列表不同 方法的重写:方法名,参数列表相同 两同:方法名相同,参数列表相同 两小:访问权限小与等于父类,返回值类型小于等于父类(返回值 ...
- Java学习笔记二.2
5.运算符:变量的意义就在于可以进行运算,运算需要用运算符来表示,分为以下几种 class test { public static void main(String[] args) { //赋值运算 ...
随机推荐
- Xcode使用心得01:断点中断问题和调整编译目标
在obj-c系列博文里,我们粗浅的介绍了obj-c的一些语法以及F库中的一些标准类的使用,但是实际编写拿得出手的APP还是得老老实实在os x上用Xcode写啊!最近上网无意中发现还有支持os x和i ...
- C# 如何在PDF文档中创建表格
表格能够直观的传达数据信息,使信息显得条理化,便于阅读同时也利于管理.那在PDF类型的文档中如何来添加表格并且对表格进行格式化操作呢?使用常规方法直接在PDF中添加表格行不通,那我们可以在借助第三方组 ...
- C# 压缩PDF图片
文档中包含图片的话,会使得整个文档比较大,占用存储空间且不利于快速.高效的传输文件.针对一些包含大量高质图片的PDF文档,若是对图片进行压缩,可以有效减少文档的占用空间.并且,在文档传输过程中也可以减 ...
- day08_Servlet学习笔记
============================================================ 一.什么是Servlet?(非常非常重要) servlet 是运行在 Web ...
- 自定义UICollectionViewLayout 布局实现瀑布流
自定义 UICollectionViewLayout 布局,实现瀑布流:UICollectionView和UICollectionViewCell 另行创建,这只是布局文件, 外界控制器只要遵守协议并 ...
- 大数据小视角1:从行存储到RCFile
前段时间一直在忙碌写毕设与项目的事情,很久没有写一些学习心得与工作记录了,开了一个新的坑,希望能继续坚持写作与记录分布式存储相关的知识.为什么叫小视角呢?因为属于随想型的内容,可能一个由小的视角来审视 ...
- sqlplus 登录数据库
sqlplus pams/pamscncc@ORCLMIS
- Zabbix如何设置脚本告警
设置告警脚本的路径 # vim /etc/zabbix/zabbix_server.confAlertScriptsPath=/usr/lib/zabbix/alertscripts 创建脚本 在这里 ...
- 【备忘】Windows的命令行下设置网络代理
在公司访问网络的时候,需要设置代理,设置浏览器的代理是相当的方便了.但有的时候要使用命令行,就需要自己设置了(貌似只要浏览器设置好了,可以直接使用命令行,但我们公司的借,需要有用户名和密码,如果没有使 ...
- 数据库中row_number()、rank()、dense_rank() 的区别
row_number的用途非常广泛,排序最好用它,它会为查询出来的每一行记录生成一个序号,依次排序且不会重复,注意使用row_number函数时必须要用over子句选择对某一列进行排序才能生成序号. ...