基于testng框架的web自动化测试

发表于:2017-7-25 17:24

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

 作者:未知    来源:博客

  package baidutest;
  import org.openqa.selenium.By;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.chrome.ChromeDriver;
  import org.openqa.selenium.firefox.FirefoxDriver;
  import org.testng.annotations.AfterClass;
  import org.testng.annotations.BeforeClass;
  import org.testng.annotations.Test;
  public class BaiduTest {
      WebDriver driver;//new FirefoxDriver();
      
      @BeforeClass
      public void setup() 
      {
          //讲谷歌驱动拷贝到项目下
          System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
          driver = new ChromeDriver();
          driver.get("https://www.baidu.com/");
          //driver.manage().window().maximize();
      }
      @Test
      public void test1()
      {
          //输入selenium查询
          driver.findElement(By.xpath(".//*[@id='kw']")).sendKeys("百度");
          //点击百度一下
          driver.findElement(By.xpath(".//*[@id='su']")).click();
          System.out.println("测试用例1");    
      }
      @Test
      public void test2()
      {
          System.out.println("测试用例2");
      }
      @AfterClass
      public void closed()
      {
          //关闭浏览器
          driver.quit();
          System.out.println("关闭浏览器成功");
      }
  }
  xpath定位元素的几种方法
  1、contains关键字
  driver.findElement(By.xpath("//a[contains(@href, 'logout')]"));
  这句话的意思是寻找页面中href属性值包含有logout这个单词的所有a元素。
  2、starts-with关键字
  driver.findElement(By.xpath("//a[starts-with(@rel, 'nofo')]"));
  这句的意思是寻找rel属性以nofo开头的a元素。其中@后面的rel可以替换成元素的任意其他属性。
  3、text使用
  driver.findElement(By.xpath("//*[text()='退出']"));
  直接查找页面当中所有的退出二字,根本就不用知道它是个a元素了。适用纯文字查找。
  4、同级目录组合查找
  driver.findElement(By.xpath("//*[@id='verificationCode']/../..//*[@id='userName']"));
  同级目录组合,../标识上级目录,//是根元素。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号