使用PageFactory初始化pageobject有什么作用呢,下面举个例子来说明

public BaiduPage baiduPage = PageFactory.initElements(driver, BaiduPage.class);

场景:使用selenium 实现自动打开www.baidu.com首页,然后在搜索框内输入“路易”,并点击查找

环境:win7,X86,IE浏览器,eclipse(安装testng插件)

步骤一:
在eclipse中创建一个maven项目,并在pom.xml中引入selenium和testng的依赖

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.selelium.css</groupId>
<artifactId>mycss</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mycss</name> <dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.48.2</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.9</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-ie-driver</artifactId>
<version>2.48.2</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

步骤二:
创建初始准备类:

package mycss;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities; public class InitPre { protected WebDriver driver;
private final String url = "www.baidu.com"; public InitPre(){
String IEPath = "C:\\Program Files\\Internet Explorer\\iexplore.exe";
//String IEPath_64 = "C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe";
String pro_Path = System.getProperty("user.dir");
String IEDriver_Path = pro_Path + "\\IEDriverServer.exe";
System.setProperty("webdriver.ie.bin", IEPath);
System.setProperty("webdriver.ie.driver", IEDriver_Path); DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability( InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
driver = new InternetExplorerDriver(ieCapabilities);
driver.get(url);
}
}

步骤3:
将百度首页Pageobject:

package mycss;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy; public class BaiduPage { @FindBy(id = "kw")
private WebElement indexInput; public void clearKeys_indexInput() {
indexInput.clear();
} public void sendkeys_indexInput(String index) {
indexInput.sendKeys(index);
} public void click_indexInput() {
indexInput.click();
}
}

步骤4:
通过PageFactory初始化PageObject对象:

package mycss;

import org.openqa.selenium.support.PageFactory;

public class InitPage extends InitPre {

    public BaiduPage baiduPage = PageFactory.initElements(driver, BaiduPage.class);

}

步骤5:
编写测试用例:

package mycss;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; public class IndexTest
{ IndexTest(){
System.out.println("IndexTest");
} public InitPage initPage=new InitPage();
BaiduPage baiduPage=initPage.baiduPage; @BeforeTest
public void inittest(){
} @Test
public void index_test(){ baiduPage.clearKeys_indexInput();
baiduPage.sendkeys_indexInput("路易");
baiduPage.click_indexInput();
}
}

完成上面的步骤后,执行IndexTest类,然后可以看到打开浏览器,进入百度首页,然后搜索“路易”
查看BaiduPage类可以知道,该类中仅对BaiduPage的控件和方法进行了封装,private WebElement indexInput indexInput并没有实例化,那么执行为什么会不报空指针呢?原因是因为下面的操作,PageFactory.initElements把driver传递给了BaiduPage的对象,当执行到WebElement indexInput indexInput这一步的话,传递的driver会在当前页面中查找该对象。大家可以尝试去掉代码中下面的一步,在执行用例查看运行结果

public BaiduPage baiduPage = PageFactory.initElements(driver, BaiduPage.class);

总结:
Selenium2 引入了PageObject的思想,将每个页面的控件和操作封装成一个个Page类,然后在实现业务逻辑时,只需要调用对应的Page类即可。
PageFactory则是为了支持PageObject而产生的,可以通过InitElements方法给Page类传递driver,当执行到WebElement时,driver在当前的页面中查找元素,这样不需要对WebElement进行实例化。如下面代码:

@FindBy(id = "kw")
private WebElement indexInput;

Selenium之使用PageFactory初始化pageobject的更多相关文章

  1. 浅析selenium的PageFactory模式 PageFactory初始化pageobject

    1.首先介绍FindBy类: For example, these two annotations point to the same element: @FindBy(id = "foob ...

  2. UI自动化测试框架(项目实战)python、Selenium(日志、邮件、pageobject)

    其实百度UI自动化测试框架,会出来很多相关的信息,不过就没有找到纯项目的,无法拿来使用的:所以我最近就写了一个简单,不过可以拿来在真正项目中可以使用的测试框架. 项目的地址:https://githu ...

  3. Appium使用PageFactory初始化对象时报空指针错误

    自己的测试框架里面,每个app页面都要初始化appium field,所以想到使用一个静态的变量,后来初始化一个页面对象时总是报空指针. 在网上找了好多材料,看着没有什么区别.后来在github上面看 ...

  4. 使用firefoxprofile,selenium设置firefox,初始化firefox

    1.什么是firefoxprofile 简单的来说就是个人定制,比如你设置自己访问主页,禁用加载图片这些个性化的设置,都可以保存到一个文件夹下,就是firefoxprofile,下次使用时候,加载该f ...

  5. 【python+selenium的web自动化】- PageObject模式解析及案例

    如果想从头学起selenium,可以去看看这个系列的文章哦! https://www.cnblogs.com/miki-peng/category/1942527.html PO模式 ​ Page O ...

  6. appium():PageObject&PageFactory

    Appium Java client has facilities which components to Page Object design pattern and Selenium PageFa ...

  7. [小北De编程手记] : Lesson 08 - Selenium For C# 之 PageFactory & 团队构建

    本文想跟大家分享的是Selenium对PageObject模式的支持和自动化测试团队的构建.<Selenium For C#>系列的文章写到这里已经接近尾声了,如果之前的文章你是一篇篇的读 ...

  8. 关于selenium的CI、框架……

    这段时间除了项目测试外,主要在做web自动化的事情,大致总结一下吧,总体的设计模式pageobject+pagefactory+testng的数据驱动,项目用maven来构建,使用jenkins集成, ...

  9. python+selenium自动化框架搭建

    环境及使用软件信息 python 3 selenium 3.13.0 xlrd 1.1.0 chromedriver HTMLTestRunner 说明: selenium/xlrd只需要再pytho ...

随机推荐

  1. GitHub 上 57 款最流行的开源深度学习项目【转】

    GitHub 上 57 款最流行的开源深度学习项目[转] 2017-02-19 20:09 334人阅读 评论(0) 收藏 举报 分类: deeplearning(28) from: https:// ...

  2. [浪风分享] PHP开发必看 我现在是这样编程的

    我在做什么 曾经,我试过接到一些需求.一眼带过后,脑袋马上随着高昂的斗志沉溺在代码的世界中 ,马不停蹄地敲着键盘直到最后测试的完成.我从思绪中恢复过来,乍一看自己写的功能,和需求差了十万八千里,我TM ...

  3. 【BZOJ】1061: [Noi2008]志愿者招募(费用流+数学)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1061 好神的一题! 学会了一种建模方式: 当方程组内的任意变量都在其中两个方程出现且一正一负,可以建 ...

  4. Loadrunner_http长连接设置

    最近协助同事解决了几个问题,也对loadrunner的一些设置加深了理解,关键是更加知其所以然. ljonathan http://www.51testing.com/html/48/202848-2 ...

  5. 系统管理模块_岗位管理_实现CRUD功能的具体步骤并设计Role实体

    系统管理模块_岗位管理_实现CRUD功能的具体步骤并设计Role实体 1,设计实体/表 设计实体 --> JavaBean --> hbm.xml --> 建表 设计Role实体 p ...

  6. malloc free, new delete 的异同点

    相同点: 都可以动态的申请并释放内存 不同点: 1. 用法不同 <1> malloc 函数为 void* malloc(size_t size), 用于申请一块长度为 size 字节的内存 ...

  7. solr初认识

    Solr : Search On Lucene Replication Solr 基本概况 Apache Solr (读音: SOLer) 是一个开源的搜索服务器.Solr 使用 Java 语言开发, ...

  8. 报错:org.springframework.http.converter.json.MappingJacksonHttpMessageConverter

    org.springframework.http.converter.json.MappingJacksonHttpMessageConverter   1.错误描述 严重: Servlet /hux ...

  9. 84、PullToRefresh使用详解

    PullToRefresh使用详解(一)--构建下拉刷新的listView http://blog.csdn.net/harvic880925/article/details/17680305 Pul ...

  10. php实现简单的权限管理

    今天主要来实现一个权限管理系统,它主要是为了给不同的用户设定不同的权限,从而实现不同权限的用户登录之后使用的功能不一样,首先先看下数据库 总共有5张表,qx_user,qx_rules和qx_jues ...