Selenium 使用要点记录<一>

本文介绍了Selenium自动化测试框架的基本概念及使用方法,并通过实例演示如何抓取网页评论,还探讨了Selenium的历史和发展,以及作者在项目中遇到的实际问题。

1. Selenium 到底是个啥高级货?

Selenium是针对Web application的一个开源的自动化测试框架,其实大家可以去Selenium官网去仔细看看哈,

大家可以顺带去提高哈自己的英语水平。当然selenium虽然开发出来的目的是为了方便大家自动化测试,但是它的功能不仅局限于这些,只要Selenium适用您的场景,它就能发挥自己的强大功能。比如上次,坐我旁边的小伙子说:我要研究一个爬虫算法,去淘宝上把关于某个宝贝的所有评论都抓下来,然后再对评论进行分析balabalabala...经过2个晚上的研究,他还是发现自己写算法啊,正则啊去分析爬到的内容取出评论果然有点疼,有点忧伤啊。然后他发现本丝正坐在位置上傻比的看着屏幕,浏览器不停的自己做操作,然后很好奇的问我,selenium能不能去taobao页面上抓点评论啥的。然后我木讷的说:骚年,不要急。容我3min来试一把。

String baseUrl = "http://detail.tmall.com/item.htm?spm=a2106.m874.1000384.1.CrEIEH&id=37505213894&source=dou&scm=1029.newlist-0.1.51108009&ppath=&sku=&ug=";
		String ffpath = "F:/ff22/firefox.exe";
		File pathToBinary = new File(ffpath);
		FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
		FirefoxProfile ffProfile = new FirefoxProfile();
		WebDriver driver = new FirefoxDriver(ffBinary,ffProfile);
	
		logger.info("Start to open " + baseUrl);

		driver.get(baseUrl);
		driver.manage().window().maximize();

		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//a[@href='#J_Reviews']")).click();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

		List<WebElement> elements = driver.findElements(By.className("tm-rate-fulltxt"));
		for (WebElement element : elements) {
			
			logger.info(element.getText());
		}

反正我是随便帮他折腾下然后评论就能很方便的取到了,然后这位骚年就表示淡淡的忧伤去了。

所以说只要你有了资源,你能去善于利用资源,你就能发现自己有更多的能力,哈哈。

2. Selenium的一些历史知识,以及我碰到的需求

其实Selenium有2个比较大的版本,最开的Selenium里边使用的是Remote control,如果使用remote control的话就需要额外的去起一个stand alone的server来支持Selenium去启动浏览器。可能考虑到对js支持以及项目的重构吧,后来基本弃用了remote control开始使用WebDriver,当然Selenium org为这2各种大的版本提供了兼容性的做法,假设有些自动化测试的项目因为某些特殊的功能WebDriver不能提供满意的效果,不妨可以试试Remote Control的Selenium接口,WebDriver和Remote Control Selenium兼容如下:

  1. WebDriver driver = new FirefoxDriver();
    driver.get(url);
    WebDriverBackedSelenium selenium = new WebDriverBackedSelenium(driver, driver.getCurrentUrl());

Selenium 现在基本上能支持现在各公司主要使用的浏览器,具体的支持兼容情况大家转link (Selenium 浏览器兼容情况)飞过去瞅瞅哈,本丝就偷个懒不再整理。

扯完了selenium的历史小问题,现在吐槽下我碰到的需求问题。

第一次弄selenium的时候是用的remote control给一个flex做的项目做一些简单的功能性测试,基本上是在每次release上线以后测试每个重要的常用元素(如连接,输入框,button)都能够正常工作(其实最重要的是给tester们声称report,只有report才是他们关注的)。其实碰到的主要问题还是元素的定位和二级menu的选择问题,但是刚做这些功能的时候是各种问题纠结啊,现在想来WebDriver是使用稍微方便。由于那个时候对remote control的接触不是很深入,功能的对比大家只能去google了。

可能真的是缘分的引导,现在项目里边down time时间又让我接着去负责一个项目里边的自动化测试项目。我琢磨:嘿,还真是信了这个邪。第二个自动化测试脚本的项目其实也很简单,跟第一个项目的功能也很类似:按照提供的操作步骤去完成对web application页面的操作,若失败则用java mail给team lead group发mail提醒。其实我这工作也不算难,但是我们需要自动化的那些个步骤真心的是长啊,搞的本来玉树临风的我每次下班回家都感觉被秋风刮了一遍又一遍的感觉。

后边也会整理在这过程过碰到的一些问题和解决方法。

3. Selenium的简答使用

其实吧,本来这不是扫盲帖的,但是可能有些同学看了也想玩一把的话我就写个简单的例子。

首先贴上使用selenium WebDriver需要依赖的包,直接奉上maven依赖了:

        <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<selenium.version>2.40.0</selenium.version>
		<selenium.driver.artifactid>selenium-java</selenium.driver.artifactid>
	</properties>

	<dependencies>
		<!-- Selenium dependency -->
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>${selenium.driver.artifactid}</artifactId>
			<version>${selenium.version}</version>
		</dependency>
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-server</artifactId>
			<version>${selenium.version}</version>
		</dependency>
		<!-- log4j dependency -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<!-- slf4j-log4j -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.5</version>
		</dependency>

		<!-- commons-lang dependency -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.3</version>
		</dependency>
    </dependencies>

测试代码:

        public void testSelenium() {
		FirefoxProfile ffProfile = new FirefoxProfile();
		WebDriver driver = new FirefoxDriver(ffProfile);
                String baseUrl = "http://www.baidu.com";
		driver.get(baseUrl);
		WebElement search_input = driver.findElement(By.id("kw1"));
		WebElement search_button = driver.findElement(By.id("su1"));
		search_input.sendKeys("馋嘴熊爱吃蜜");
		search_button.click();
		try {
			Thread.sleep(10000L);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally{
			driver.quit();
		}
	}

这就是简单的使用啦,我这边使用的是firefox,如果您在测试的时候错误的话可以去查看:

1) selenium版本和浏览器版本不兼容

可以用如下方式重新指定浏览器安装路径:

a. 

                String ffpath = "F:/ff22/firefox.exe";
		File pathToBinary = new File(ffpath);
		FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
		FirefoxProfile ffProfile = new FirefoxProfile();
		WebDriver driver = new FirefoxDriver(ffBinary,ffProfile);

b.

System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");

不同的浏览器去生成WebDriver都有很小的差别,以前Chrome好像还需要额外的导入一个driver包,由于最近都是使用firefox大家可以自行Google。

最最简单的介绍到此结束。本来想直接给记录自己碰到的问题的,结果花了这么久才写了这么点,只能留后慢慢记啦。

本丝第一次写blog,希望各路大神指正哈。

转载于:https://my.oschina.net/athhu/blog/272365

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值