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


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

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

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

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

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

      1 package com.qiujy.testweb_mvn; 
      2 import java.io.File;
      3 import java.io.IOException;
      4 import java.util.List;
      5 
      6 import org.apache.commons.io.FileUtils;
      7 import org.openqa.selenium.By;
      8 import org.openqa.selenium.Cookie;
      9 import org.openqa.selenium.OutputType;
     10 import org.openqa.selenium.TakesScreenshot;
     11 import org.openqa.selenium.WebDriver;
     12 import org.openqa.selenium.WebElement;
     13 import org.openqa.selenium.firefox.FirefoxDriver;
     14 import org.openqa.selenium.firefox.FirefoxOptions;
     15 import org.openqa.selenium.firefox.FirefoxProfile;
     16 import org.openqa.selenium.firefox.internal.ProfilesIni;
     17 import org.openqa.selenium.support.ui.ExpectedCondition;
     18 import org.openqa.selenium.support.ui.WebDriverWait;
     19 
     20 
     21 public class rememberme {
     22     private static final String WebElement = null;
     23 
     24     /**
     25      * @param args
     26      * @throws InterruptedException
     27      * @throws IOException
     28      */
     29     public static void main(String[] args) throws InterruptedException, IOException {
     30            System.setProperty("webdriver.gecko.driver", "D:\geckodriver-v0.19.1-win64\geckodriver.exe");
     31            ProfilesIni pi = new ProfilesIni();
     32            FirefoxProfile profile = pi.getProfile("default");
     33            FirefoxOptions options = new FirefoxOptions();
     34            options.setProfile(profile);
     35         WebDriver driver = new FirefoxDriver(options);
     36 
     37            
     38         driver.manage().window().maximize();
     39         driver.get("http://xxxxx/login");   
     40         String sysTitle = driver.getTitle();
     41         String expectedTitle="Dva Demo";
     42         if (sysTitle.equals(expectedTitle)) {
     43             System.out.println(sysTitle);
     44         } else {
     45             System.out.println("Wrong Title");
     46         }
     47           
     48         //点“注册”,打开注册页面
     49         WebElement addButton=driver.findElement(By.xpath("//*[@id="root"]/div/form/div[5]/a"));
     50         addButton.click();
     51            Thread.sleep(1000);
     52            
     53         //输入注册信息
     54         WebElement userName=driver.findElement(By.xpath("//*[@id="nickname"]"));
     55         userName.sendKeys("abc");
     56         WebElement password=driver.findElement(By.xpath("//*[@id="password"]"));
     57         password.sendKeys("123456");
     58         WebElement cpassword=driver.findElement(By.xpath("//*[@id="confirm"]"));
     59         cpassword.sendKeys("123456");
     60         WebElement mailbox=driver.findElement(By.xpath("//*[@id="email"]"));
     61         mailbox.sendKeys("abc@000.com");
     62         WebElement add=driver.findElement(By.xpath("//*[@id="residence"]"));
     63         add.click();
     64         WebElement add1=driver.findElement(By.xpath("/html/body/div[2]/div/div/div/ul[1]/li[1]"));
     65         add1.click();    
     66         System.out.println(add1);
     67         WebElement add2=driver.findElement(By.xpath("/html/body/div[2]/div/div/div/ul[2]/li"));
     68         add2.click();    
     69         System.out.println(add2);
     70         WebElement add3=driver.findElement(By.xpath("/html/body/div[2]/div/div/div/ul[3]/li"));
     71         add3.click();    
     72         System.out.println(add3);
     73         WebElement cellphone=driver.findElement(By.xpath("//*[@id="phone"]"));
     74         cellphone.sendKeys("123456789");    
     75  
     76         //获取验证码
     77         WebElement verifycode=driver.findElement(By.className("ant-btn"));
     78         verifycode.click();
     79         WebElement text=driver.findElement(By.className("ant-notification-notice-description"));
     80         String str=text.getText();
     81         String str1=str.replaceAll("请在5min内输入验证码:","");
     82         System.out.println(str1);
     83         WebElement inputcode=driver.findElement(By.xpath("//*[@id="captcha"]"));
     84         inputcode.sendKeys(str1);
     85         WebElement checkbox=driver.findElement(By.xpath("//*[@id="root"]/div/form/div[8]/div/div/label/span[1]/input"));
     86         checkbox.click();
     87         File screenshotFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
     88         FileUtils.copyFile(screenshotFile, new File("E:\screenshot\Register"+System.currentTimeMillis()+".png"));
     89         WebElement register=driver.findElement(By.xpath("//*[@id="root"]/div/form/div[9]/div/div/button"));
     90         register.click();
     91 
     92         //取出注册成功的提示字样
     93         WebElement text2=(new WebDriverWait(driver,10)).until(new ExpectedCondition<WebElement>() {
     94             public org.openqa.selenium.WebElement apply(WebDriver driver) {
     95                 List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-description"));
     96                 if(eles.size() == 2 && eles.get(1).isDisplayed()) {
     97                     return eles.get(1);
     98                 }
     99                 return null;
    100             }
    101         });
    102 
    103         System.out.println(text2.getText().toString());    
    104 
    105         Thread.sleep(1000);
    106         File screenshotFile1=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    107         FileUtils.copyFile(screenshotFile1, new File("E:\screenshot\Success"+System.currentTimeMillis()+".png"));
    108         
    109         
    110         
    111         
    112         
    113         //用刚刚注册的帐号登录
    114         WebElement uName=driver.findElement(By.xpath("//*[@id="userName"]"));
    115         uName.sendKeys("abc");
    116         WebElement pwd=driver.findElement(By.xpath("//*[@id="password"]"));
    117         pwd.sendKeys("123456");    
    118         WebElement Button=driver.findElement(By.xpath("//*[@id="root"]/div/form/div[4]/button"));
    119         Button.click();
    120         File screenshotFile2=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    121         FileUtils.copyFile(screenshotFile2, new File("E:\screenshot\login"+System.currentTimeMillis()+".png"));
    122         
    123         
    124         
    125         //取出登录成功的提示框title
    126         WebElement text3=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
    127             public org.openqa.selenium.WebElement apply(WebDriver driver){
    128                 List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-message"));
    129                 if(eles.size()==3 && eles.get(2).isDisplayed()) {
    130                     return eles.get(2);
    131                 }
    132                 return  null;
    133             }
    134         });
    135         System.out.println(text3.getText().toString());
    136         
    137         
    138         
    139         
    140         //取出登录成功的提示字样
    141         WebElement text4=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
    142             public org.openqa.selenium.WebElement apply(WebDriver driver){
    143                 List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-description"));
    144                 if(eles.size()==3 && eles.get(2).isDisplayed()) {
    145                     return eles.get(2);
    146                 }
    147                 return  null;
    148             }
    149         });
    150         System.out.println(text4.getText().toString());
    151             
    152         File screenshotFile3=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    153         FileUtils.copyFile(screenshotFile3, new File("E:\screenshot\LS"+System.currentTimeMillis()+".png"));        
    154         
    155         Thread.sleep(10000);
    156         
    157 //        重新登录的时候因为已经记忆了密码和帐号信息, 所以不用再次输入, 直接点login就好
    158         driver.get("http://xxxxx/login");   
    159 //        WebElement uName2=driver.findElement(By.xpath("//*[@id="userName"]"));
    160 //        uName2.sendKeys("abc");
    161 //        WebElement pwd2=driver.findElement(By.xpath("//*[@id="password"]"));
    162 //        pwd2.sendKeys("123456");    
    163         File screenshotFile4=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    164         FileUtils.copyFile(screenshotFile4, new File("E:\screenshot\relogin"+System.currentTimeMillis()+".png"));
    165         WebElement Button2=driver.findElement(By.xpath("//*[@id="root"]/div/form/div[4]/button"));
    166         Button2.click();
    167                 WebElement text5=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
    168             public org.openqa.selenium.WebElement apply(WebDriver driver){
    169                 List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-description"));
    170                 if(eles.size()==2 && eles.get(1).isDisplayed()) {
    171                     return eles.get(1);
    172                 }
    173                 return  null;
    174             }
    175         });
    176         System.out.println(text5.getText().toString());        
    177         Thread.sleep(1000);
    178         File screenshotFile5=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    179         FileUtils.copyFile(screenshotFile5, new File("E:\screenshot\reloginpost"+System.currentTimeMillis()+".png"));
    180         
    181         
    182         
    183         
    184 //        再次打开登录页面,输入帐户密码(因系统已记忆 ,再输入会造成帐户密码信息double, 如帐户变成: abcabc)
    185         driver.get("http://xxxxx/login");   
    186         WebElement uName2=driver.findElement(By.xpath("//*[@id="userName"]"));
    187         uName2.sendKeys("abc");
    188         WebElement pwd2=driver.findElement(By.xpath("//*[@id="password"]"));
    189         pwd2.sendKeys("123456");    
    190         File screenshotFile6=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    191         FileUtils.copyFile(screenshotFile6, new File("E:\screenshot\relogin2"+System.currentTimeMillis()+".png"));
    192         WebElement Button3=driver.findElement(By.xpath("//*[@id="root"]/div/form/div[4]/button"));
    193         Button3.click();
    194         Thread.sleep(1000);
    195 //        实际出现了三个提示框 ,前两个显示登录成功, 最后一个显示失败,但是仍然打开了内容页。
    196         //取出第一个提示框title
    197         WebElement text6=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
    198             public org.openqa.selenium.WebElement apply(WebDriver driver){
    199                 List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-message"));
    200                 if(eles.size()==2 && eles.get(0).isDisplayed()) {
    201                     return eles.get(0);
    202                 }
    203                 return  null;
    204             }
    205         });
    206         System.out.println(text6.getText().toString());
    207         
    208         
    209         
    210         
    211         //取出第一个提示框内容
    212         WebElement text7=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
    213             public org.openqa.selenium.WebElement apply(WebDriver driver){
    214                 List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-description"));
    215                 if(eles.size()==2 && eles.get(0).isDisplayed()) {
    216                     return eles.get(0);
    217                 }
    218                 return  null;
    219             }
    220         });
    221         System.out.println(text7.getText().toString());        
    222         
    223         
    224         
    225         //取出第二个提示框title
    226         WebElement text8=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
    227             public org.openqa.selenium.WebElement apply(WebDriver driver){
    228                 List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-message"));
    229                 if(eles.size()==2 && eles.get(1).isDisplayed()) {
    230                     return eles.get(1);
    231                 }
    232                 return  null;
    233             }
    234         });
    235         System.out.println(text8.getText().toString());
    236         
    237         
    238         
    239         
    240         //取出第二个提示框内容
    241         WebElement text9=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
    242             public org.openqa.selenium.WebElement apply(WebDriver driver){
    243                 List<org.openqa.selenium.WebElement> eles = driver.findElements(By.className("ant-notification-notice-description"));
    244                 if(eles.size()==2 && eles.get(1).isDisplayed()) {
    245                     return eles.get(1);
    246                 }
    247                 return  null;
    248             }
    249         });
    250         System.out.println(text9.getText().toString());    
    251         
    252         Thread.sleep(1000);
    253         File screenshotFile7=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    254         FileUtils.copyFile(screenshotFile7, new File("E:\screenshot\reloginpost"+System.currentTimeMillis()+".png"));
    255         
    256         
    257         
    258         
    259          driver.quit();
    260      
    261     }
    262 
    263 }
    264 
    265 /**
    266 *@author: Created by Qian
    267 *@date: 2017年12月27日 268 *@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. 269 **@answer: 270 *@action: 271 */
  • 相关阅读:
    代码生成器
    怎样成为优秀的软件模型设计者[转]
    2010年部分节假日安排的通知
    Web开发工具整理
    JS模版
    Timer_MinBytesPerSecond,Timer_ConnectionIdle解决办法
    [原]Asp.Net提交后,刷新时事件触发问题解决
    [转]JS日期选择控件
    知道了sa密码,控制服务器
    调试
  • 原文地址:https://www.cnblogs.com/cheese320/p/8183939.html
Copyright © 2020-2023  润新知