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) { //赋值运算 ...
随机推荐
- vs2010修改状态栏的CStatusBar指针的的SetPaneText()方法时死活不对问题
vs2010的mfc在有些地方不太一样不容易注意到,今天在修改状态栏的时候,就碰见了问题,死活修改不了. 参照下面的帖子: 点击打开链接 : 使用VS2010更改MFC程序的状态栏 2011-04-1 ...
- 为何我会喜欢封闭的apple?
原来本猫喜欢的手机是简单的塞班系统,nokia的E72i,超经典吧!就是最近都有把它充满电重新拿出来用的冲动呀.可惜无奈的是上面的应用太少呀!原来PC和笔记本装的是各种清一色的windows系统,从3 ...
- 用python-webdriver实现自动填表
在日常工作中常常需要重复填写某些表单,如果人工完成,费时费力,而且网络延迟令人十分崩溃.如果能够用程序实现自动填表,效率可以提高一倍以上,并且能够移植到多台计算机,进一步提高工作效率.webdrive ...
- win7 64位专业版下的x64编译问题
在Django的开发过程中,碰到一个问题,就是所有本地库的位数必须是相同的,于是某些库需要重新编译一下,工作环境,不能用盗版程序,VC++ 2008\2010 Express版本身都不支持X64的编译 ...
- Github Page 绑定域名
http://kyle.xlau.org/posts/github-cname.html CNAME 创建一个CNAME文件,内容是你的域名,如: xlau.org 然后把此文件添加到Github仓库 ...
- C++笔记019:C++中的const修饰的是一个真正的常量
原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 程序一: 我们知道数组的下标不能为变量,必须是一个确定的值.在C语言中看程序: #define a 10 int main() { //第 ...
- RocketMQ源码 — 九、 RocketMQ延时消息
上一节消息重试里面提到了重试的消息可以被延时消费,其实除此之外,用户发送的消息也可以指定延时时间(更准确的说是延时等级),然后在指定延时时间之后投递消息,然后被consumer消费.阿里云的ons还支 ...
- 洛谷 P1057 解题报告
P1057 传球游戏 题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏. 游戏规则是这样的:n个同学站成一个圆圈,其中的一个同学手里拿着一个球,当老师吹 ...
- 团队项目第二阶段个人进展——Day9
一.昨天工作总结 冲刺第九天,完成图片的优化,与队友一起讨论如何合并并优化 二.遇到的问题 无 三.今日工作规划 合并后优化
- js万年历,麻雀虽小五脏俱全,由原生js编写
对于前端来说,我们可能见到最多的就是各种各样的框架,各种各样的插件了,有各种各样的功能,比如轮播啊,日历啊,给我们提供了很大的方便,但是呢?我们在用别人这些写好的插件,框架的时候,有没有试着问一问自己 ...