Selenium WebDriver上创建WebDriver测试脚本

发表于:2019-1-21 11:26

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:桃子    来源:51testing

   本文实现一个WebDriver测试脚本,介绍WebDrive的常用命令、UI元素定位的策略以及在脚本中的使用,还有Get命令。
  你将学到:
  · 脚本创建
  · 代码走查
  · 测试执行
  · 定位Web元素
  · 定位符类型及其语法
  · 总结
  一.脚本创建
  脚本创建部分仍然使用之前创建的“Learning Selenium”项目和“gmail.com”作为被测试应用程序(AUT)。
  场景:
  启动浏览器,打开“Gmail.com”。
  验证页面标题并打印验证结果。
  输入用户名和密码。
  单击登录按钮。
  关闭web浏览器。
  步骤1:在“Learning Selenium”项目下创建一个名为“Gmail_Login”的新java类名称
  步骤2:在“Gmail_Login”中复制并粘贴以下代码
   import org.openqa.selenium.By;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.firefox.FirefoxDriver;
  public class Gmail_Login {
  /**
  * @param args
  */
  public static void main(String[] args) {
  // objects and variables instantiation
  WebDriver driver = new FirefoxDriver();
  String appUrl = "https://accounts.google.com";
  // launch the firefox browser and open the application url
  driver.get(appUrl);
  // maximize the browser window
  driver.manage().window().maximize();
  // declare and initialize the variable to store the expected title of the webpage.
  String expectedTitle = " Sign in - Google Accounts ";
  // fetch the title of the web page and save it into a string variable
  String actualTitle = driver.getTitle();
  // compare the expected title of the page with the actual title of the page and print the result
  if (expectedTitle.equals(actualTitle))
  {
  System.out.println("Verification Successful - The correct title is displayed on the web page.");
  }
  else
  {
  System.out.println("Verification Failed - An incorrect title is displayed on the web page.");
  }
  // enter a valid username in the email textbox
  WebElement username = driver.findElement(By.id("Email"));
  username.clear();
  username.sendKeys("TestSelenium");
  // enter a valid password in the password textbox
  WebElement password = driver.findElement(By.id("Passwd"));
  password.clear();
  password.sendKeys("password123");
  // click on the Sign in button
  WebElement SignInButton = driver.findElement(By.id("signIn"));
  SignInButton.click();
  // close the web browser
  driver.close();
  System.out.println("Test script executed successfully.");
  // terminate the program
  System.exit(0);
  }
  }
  上面的代码的意思与前面的文本场景相同
  二.  代码走查
  导入语句:
   import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.firefox.FirefoxDriver;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.By;
  在编码前,我们需要导入上面的包:
  import org.openqa.selenium.WebDriver:引用WebDriver的接口,该接口用于实例化一个新的Web 浏览器
  import org.openqa.selenium.firefox.FirefoxDriver:引用FirefoxDriver类,该类在WebDriver的接口上实例化Firefox特定驱动程序
  import org.openqa.selenium.WebElement:引用WebElement类,该类用于实例化一个新的web元素。
  import org.openqa.selenium.By:引用By 类,用于调用定位符
  有时我们需要引入其他几个包来实现更复杂、更独特的功能,如excel操作、数据库连接、日志记录、断言等
  对象实例化
  WebDriver driver = new FirefoxDriver();
  创建一个引用变量WebDriver,并使用FirefoxDriver类实例化它。这个过程将启动一个默认的Firefox配置文件,而不加载任何扩展和插件。
  启动Web浏览器
  driver.get(appUrl);
  在WebDriver实例上调用get()方法来启动新的web浏览器实例。Get()方法的字符串将web浏览器重定向应用程序的URL
  浏览器窗口最大化
  driver.manage().window().maximize();
  maximize()方法是在浏览器窗口被重定向到应用程序URL后,将其最大化。
  获取页面标题
  driver.getTitle ();
  获取当前网页的标题,可以将获取的标题加载到字符串变量中。
   if (expectedTitle.equals(actualTitle))
  {
  System.out.println("Verification Successful - The correct title is displayed on the web page.");
  }
  else
  {
  System.out.println("Verification Failed - An incorrect title is displayed on the web page.");
  }
  上面代码的意思是用java结构将期望的结果和实际的结果进行比较,根据比较的结果进行输出。
  WebElement实例化
  WebElement username = driver.findElement(By.id(“Email”));
  在上面的语句中,我们通过调用“driver.findElement(By.id(“Email”))”,实例化了WebElement的引用。同时,用户名可以通过引用用户界面上的电子邮件文本框,实现对用户界面的一些操作
  清除命令
  username. Clear();
  clear()方法/命令用于清除文本框中出现的值,包括清除默认值
  sendKeys命令
  username.sendKeys(“TestSelenium “);
  sendKeys()方法/命令用于在文本框中输入/键入指定的值,上面的代码意思是在Gmail应用程序的电子邮件文本框中输入字符串“TestSelenium”, sendKeys是webdriver脚本中最常用的命令之一。
  Click命令
  SignInButton.click();
  与sendKeys()类似,click()是另一个与web元素交互的命令。
  单击()命令/方法用于单击web页面上的web元素。
  上面的代码意思是在Gmail应用程序上单击“Sign in”按钮
  注:
  与sendKeys()方法不同,click()方法不能参数化。
  为了支持单击web元素可能会加载一个新页面这种情况,click()方法是等待页面加载的编码方式。
  关闭Web浏览器
  driver.close();
  close()用于关闭当前浏览器窗口。
  终止Java程序
  System.exit(0);
  Exit()方法强制终止Java程序。记住在终止Java程序之前关闭所有浏览器实例。
  三.测试执行
  可以有下面3种方式来执行脚本:
  1.在eclipse的菜单栏点击执行按钮运行测试脚本,参见下图
  2.在编辑器任意地方邮件点击,选择“Run As”选项,接下来选择“Java Application”
  3.或者采用快捷键方式,按下Ctril+F11组合键
  执行成功后,在面板上显示“Test script executed successfully”
  四.定位Web元素
  WebDriver中的Web元素定位和检查可以像在Selenium IDE的前面文章中介绍的那样,使用Selenium IDE和Firebug可以检查GUI上的web元素。强烈建议使用Selenium IDE来查找web元素。找到web元素后,复制并粘贴目标值到WebDriver代码中。
  在WebDriver中,web元素是在动态查找器(findElement)的帮助下定位的(findElement(By.locatorType(“locator value”))).
  比如:
  driver.findElement(By.id(“Email”));
  定位符类型及其语法
  五.总结
  在本文中,我们使用WebDriver和Java开发了一个自动化脚本。我们还讨论了构成WebDriver脚本的各种组件。
  重点内容
  在编写脚本前,我们需要导入一些能够创建WebDriver脚本的包
importopenqa.selenium.By;
importopenqa.selenium.WebDriver;
importopenqa.selenium.WebElement;
importopenqa.selenium.firefox.FirefoxDriver;
  · get()方法打开新的浏览器,get()方法的字符串将启动web浏览器并重定向到应用程序的URL
  · maximize()方法使窗口最大化
  · clear() 方法可清楚文本框里的任何内容
  · sendKeys() 方法在文本框中输入指定的值
  · Click()方法用于在web页面上点击web元素
  · 在WebDriver中,可以使用动态查找器定位web元素
  可用的定位器类型:
  · id
  · className
  · name
  · xpath
  · cssSelector
  · linkText
  · partialLinkText
  · tagName
 
      相关推荐:《51测试天地—原创测试文章系列(五十二)》
      版权声明:本文出自51Testing原创,51Testing软件测试网及相关内容提供者拥有内容的全部版权,未经明确的书面许可,任何人或单位不得对本网站内容复制、转载或进行镜像,否则将追究法律责任。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号