欢迎访问昆山宝鼎软件有限公司网站! 设为首页 | 网站地图 | XML | RSS订阅 | 宝鼎邮箱 | 后台管理


新闻资讯

MENU

软件开发知识

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

点击: 次  来源:宝鼎软件 时间:2017-07-28

原文出处: 小坦克

Web 开拓人员凡是需要操作JavaScript弹出对话框来给用户一些信息提示,软件开发, 包罗以下几种范例

对话框范例

1. 告诫框: 用于提示用户相关信息的验证功效, 错误或告诫等

 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(); } 附: selenium java教程 (连载中

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

 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(); } 附: selenium java教程 (连载中

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(); } 附: selenium java教程 (连载中 

测试页面

用如下页面为例举办讲授,包罗了告诫框,提示框,劳务派遣管理系统,软件开发,确认框。

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

 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(); } 附: selenium java教程 (连载中 

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();
    }

附: selenium java教程 (连载中, 敬请等候)

java selenium (一) selenium 先容

java selenium (二) 情况搭建要领一

java selenium (三) 情况搭建 基于Maven

java selenium (四) 利用欣赏器调试东西

java selenium (五) 元素定位大全

java selenium (六) xpath 定位

java selenium (七) CSS 定位

java selenium (八) Selenium IDE 用法

java selenium (九) 常见web UI 元素操纵 及API利用

java selenium (十) 操纵欣赏器

java selenium (十一) 操纵弹出对话框

java selenium (十二) 操纵弹出窗口

java selenium (十三) 智能期待页面加载完成

java selenium (十四) 处理惩罚Iframe 中的元素