10分钟玩转Selenium自动化

  大家好,今天小编向大家介绍一款一直很火的自动化测试工具Selenium。我们将教您如何在10分钟内完成Selenium的环境搭建,玩转POM,跑起DEMO,走向人生巅峰。

  Selenium automates browsers. That’s it! 

  – Selenium官网介绍

  1、搭建项目

  ●下载并安装Java JDK

  ●下载并安装IntelliJ IDEA

  ●在IntelliJ IDEA中创建新的Maven project: File > New > Project > Maven

  ●将“Project SDK”关联至本机JDK,路径一般为“C:\Program Files\Java\jdkxxxx”

  ●设置groupId与artifactId:

  <groupId>SeleniumTest</groupId>

  <artifactId>Test</artifactId>

  ●在项目左侧面板的pom.xml文件中添加Selenium和JUnit依赖项

  <dependencies>

  <!-- JUnit -->

  <dependency>

  <groupId>junit</groupId>

  <artifactId>junit</artifactId>

  <version>4.12</version>

  <scope>test</scope>

  </dependency>

  <!-- Selenium -->

  <dependency>

  <groupId>org.seleniumhq.selenium</groupId>

  <artifactId>selenium-firefox-driver</artifactId>

  <version>3.7.1</version>

  </dependency>

  <dependency>

  <groupId>org.seleniumhq.selenium</groupId>

  <artifactId>selenium-support</artifactId>

  <version>3.7.1</version>

  </dependency>

  <dependency>

  <groupId>org.seleniumhq.selenium</groupId>

  <artifactId>selenium-java</artifactId>

  <version>3.7.1</version>

  </dependency>

  </dependencies>

  直到这一步,如果前面都配置正确,依赖会自动进行下载。如果没有自动下载,则在IDEA的左侧Maven Projects面板找到并激活Plugins > Install > install。

  一旦项目成功完成装载,在”src/test/java”目录下创建”com.sogo”目录,并在其下创建”com.sogo.tests”和”com.sogo.webpages”目录。

  我们以后把Page Object/Page Factory类放入”com.sogo.webpages”目录,把测试用例放在”com.sogo.tests”目录下。接下来,我们创建Page Object类。

   

  2、创建主页 Page Object

  首先之先,在”com.sogo.webpages”下创建一个叫做”HomePage”的类。

  package com.sogo.webpages;

  import org.openqa.selenium.WebDriver;

  import org.openqa.selenium.WebElement;

  import org.openqa.selenium.support.FindBy;

  import org.openqa.selenium.support.How;

  import org.openqa.selenium.support.PageFactory;

  public class HomePage {

  private WebDriver driver;

  //Page URL

  private static String PAGE_URL="https://weixin.sogo.com";

  //Locators

  //Find Weixin Button

  @FindBy(how = How.LINK_TEXT, using = "特朗普访华")

  private WebElement weixinButton;

  //Constructor

  public HomePage(WebDriver driver){

  this.driver=driver;

  driver.get(PAGE_URL);

  //Initialise Elements

  PageFactory.initElements(driver, this);

  }

  public void clickTrumpVisitToChina(){

  weixinButton.click();

  }

  }

  3、找到元素定位器

  在搜狗微信搜索的主页里我们关注的是“特朗普访华”的消息,我们通过文本的匹配找到这个元素,也就是上文代码做的事情。

  除了需要将网页建模为页面对象类之外,查找和识别网页元素通常也会比较麻烦。我们可以通过使用Google Chrome自带的元素检查器,找到关注的任意元素的相关信息。

  在“特朗普访华”右键,点击检查元素,右键点击Copy > Copy Xpath,我们就可以找到对应的页面元素路径。

  @FindBy(xpath = "//*[@id="topwords"]/li[1]/a")

  WebElement hyperlink;

  4、创建具体信息页面Page Object

  在这个页面中,我们关心的有两点:1. 页面是否成功加载;2.进入具体信息页面

  package com.sogo.webpages;

  import org.openqa.selenium.WebDriver;

  import org.openqa.selenium.WebElement;

  import org.openqa.selenium.support.FindBy;

  import org.openqa.selenium.support.PageFactory;

  public class DetailedInfo {

  private WebDriver driver;

  @FindBy(xpath = "//*[@id=\"topwords\"]/li[1]/a")

  private WebElement hyperlink;

  @FindBy(linkText = "特朗普访华亮点有哪些?")

  private WebElement hyperlink2;

  //Constructor

  public DetailedInfo (WebDriver driver){

  this.driver = driver;

  //Initiallize Elements

  PageFactory.initElements(driver, this);

  }

  public boolean isPageOpened(){

  return hyperlink.getText().toString().contains("特朗普访华");

  }

  public void clickOnOpen(){

  hyperlink2.click();

  }

  }

  5、写一个测试用例

  Page Object类代表了各个网页与用户的一些动作,接下来,在tests目录下我们可以写一些测试用例与注入检查其生效性。

  package com.sogo.tests;

  import com.sogo.webpages.DetailedInfo;

  import com.sogo.webpages.HomePage;

  import org.junit.*;

  import org.openqa.selenium.WebDriver;

  import org.openqa.selenium.firefox.FirefoxDriver;

  import java.net.URL;

  import java.util.concurrent.TimeUnit;

  public class WhereIsTrumpTest {

  WebDriver driver;

  @Before

  public void setup(){

  driver = new FirefoxDriver();

  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

  }

  @Test

  public void testTrump(){

  HomePage home = new HomePage(driver);

  home.clickTrumpVisitToChina();

  DetailedInfo detailedInfo = new DetailedInfo(driver);

  Assert.assertTrue(detailedInfo.isPageOpened());

  detailedInfo.clickOnOpen();

  }

  @After

  public void close(){

  driver.close();

  }

  }

  这样,一个完整的测试框架和Demo就搭建完成了,你之后需要做的就是往里面添加Page Objects和测试用例,Trump也被我们找到了,你学会了吗?

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值