zoukankan      html  css  js  c++  java
  • java selenium (十一) 操作弹出对话框

    Web 开发人员通常需要利用JavaScript弹出对话框来给用户一些信息提示, 包括以下几种类型

    阅读目录

     

    对话框类型

    1.  警告框: 用于提示用户相关信息的验证结果, 错误或警告等

    2. 提示框: 用于提示用户在当前对话框中输入数据,一般需要用户单击取消或者确认按钮

    3. 确认框: 用于提示用户确认或者取消某个操作,一般需要用户单击取消或者确认按钮

     

    测试页面

    用如下页面为例进行讲解,  包括了警告框,提示框,确认框

    http://sislands.com/coin70/week1/dialogbox.htm

     

    Selenium 操作对话框的代码

        public static void testAlert(WebDriver driver)
        {
            String url="http://sislands.com/coin70/week1/dialogbox.htm";
            driver.get(url);
            
            WebElement alertButton = driver.findElement(By.xpath("//input[@value='alert']"));
            alertButton.click();
            
            Alert javascriptAlert = driver.switchTo().alert();
            System.out.println(javascriptAlert.getText());
            javascriptAlert.accept();
        }
        
        public static void testPrompt(WebDriver driver) throws Exception
        {
            String url="http://sislands.com/coin70/week1/dialogbox.htm";
            driver.get(url);
            
            WebElement promptButton = driver.findElement(By.xpath("//input[@value='prompt']"));
            promptButton.click();
            Thread.sleep(2000);
            Alert javascriptPrompt = driver.switchTo().alert();
            javascriptPrompt.sendKeys("This is learning Selenium");
            javascriptPrompt.accept();    
            
            System.out.println(javascriptPrompt.getText());
            
            javascriptPrompt=driver.switchTo().alert();
            javascriptPrompt.accept();
            
            Thread.sleep(2000);
            promptButton.click();
            javascriptPrompt=driver.switchTo().alert();
            javascriptPrompt.dismiss();
            Thread.sleep(2000);
            javascriptPrompt=driver.switchTo().alert();
            javascriptPrompt.accept();
        }
        
        public static void testConfirm(WebDriver driver) throws Exception
        {
            String url="http://sislands.com/coin70/week1/dialogbox.htm";
            driver.get(url);
            
            WebElement confirmButton = driver.findElement(By.xpath("//input[@value='confirm']"));
            confirmButton.click();
            Thread.sleep(2000);
            Alert javascriptConfirm = driver.switchTo().alert();
            javascriptConfirm.accept();
            Thread.sleep(2000);
            javascriptConfirm = driver.switchTo().alert();
            javascriptConfirm.accept();
        }
     
  • 相关阅读:
    stm32f103和stm32f407的GPIO口模式设置以及相互对应的关系
    基于STM32单片机实现屏幕休眠后OLED屏幕滚动效果
    基于51单片机的超声波模块HC-SR04的使用
    用51单片机控制L298N电机驱动模块
    学习笔记——51单片机 单片机与单片机之间的通讯
    基于51单片机的电子密码锁—1
    LCD1602学习调试
    基于51单片机,通过定时器实现的时钟程序
    有进度条圆周率计算
    python turtle 学习笔记
  • 原文地址:https://www.cnblogs.com/TankXiao/p/5260445.html
Copyright © 2011-2022 走看看