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) { //赋值运算 ...
随机推荐
- android EventBus详解(一)
EventBus 是一款针对Android优化的发布/订阅事件总线.主要功能是替代Intent, Handler, BroadCast 在 Fragment,Activity,Service,线程之间 ...
- Zeroc Ice开发环境搭建
搭建Ice环境 1. Linux(推荐,更接近真实生产环境) 2. Windows(方便学习开发) 下载安装包:https://zeroc.com/downloads (百度网盘链接:http ...
- ubuntu12.04:Tomcat 7服务器:手动安装
1.下载tomcat7.0.34. 网址:http://tomcat.apache.org/ 2.下载的文件解压在下载: 进入目录: cd /usr/local 创建目录 : sudo mkdir d ...
- reorder list(链表重新排序)
Given a singly linked list L: L0→L1→-→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do thi ...
- Maximum Subarray(最大子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 查询linux机器的公网ip
在linux终端提示符下,输入以下命令: curl members.3322.org/dyndns/getip 可以看到下图已经查询到公网IP地址了,就是这么简单
- oracle查询表索引
转载 http://blog.sina.com.cn/s/blog_5376c7190101hvvb.html 如下: select * from user_indexes where table_n ...
- spring中一些aware接口
Spring中提供一些Aware相关接口,像是BeanFactoryAware. ApplicationContextAware.ResourceLoaderAware.ServletContextA ...
- java多线程的理解
java多线程的理解 线程的5种状态:新建,就绪,运行,阻塞,死亡. Thread.sleep:线程 运行状态 转为 阻塞状态,(其它线程启动运行) Thread.yield: 线程 运行 ...
- windows环境下zookeeper安装和使用
一.简介 zooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一 ...