源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

java selenium操作弹出对话框示例讲解

  • 时间:2021-01-05 22:10 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:java selenium操作弹出对话框示例讲解
Web 开发人员通常需要利用JavaScript弹出对话框来给用户一些信息提示, 包括以下几种类型 阅读目录 [list=1] [*]对话框类型[/*] [*]测试页面[/*] [*]Selenium 操作对话框的代码[/*] [/list] [b]对话框类型[/b] 1.  警告框: 用于提示用户相关信息的验证结果, 错误或警告等 [img]http://files.jb51.net/file_images/article/201608/2016812173434162.png?2016712173455[/img] 2. 提示框: 用于提示用户在当前对话框中输入数据,一般需要用户单击取消或者确认按钮 [img]http://files.jb51.net/file_images/article/201608/2016812173525243.png?2016712173542[/img] 3. 确认框: 用于提示用户确认或者取消某个操作,一般需要用户单击取消或者确认按钮 [img]http://files.jb51.net/file_images/article/201608/2016812173630872.png?2016712173646[/img] [b]测试页面[/b] 用如下页面为例进行讲解,  包括了警告框,提示框,确认框 http://sislands.com/coin70/week1/dialogbox.htm [img]http://files.jb51.net/file_images/article/201608/2016812173745953.png?201671217384[/img] [b]Selenium 操作对话框的代码[/b]
 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();
 }
 以上就是对 java selenium操作弹出对话框的资料整理,后续继续补充,谢谢大家对本站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部