Selenium快速入门

发表于:2010-9-17 15:17

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

 作者:未知    来源:51Testing软件测试网采编

  五、多环境测试

package test;

import org.junit.Test;

import junit.framework.TestCase;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
/**
* 多浏览器测试
* @author hgz
*
*/
public class NewTest extends TestCase {
@Test
public void testNew() throws Exception {
Selenium selenium = new DefaultSelenium("localhost", 4444, "*chrome",
"http://www.google.cn");
script(selenium);
}

@Test
public void testNew2() throws Exception {
Selenium selenium = new DefaultSelenium("localhost", 4444, "*iehta",
"http://www.google.cn");
script(selenium);
}

private void script(Selenium selenium) throws Exception {
try {
selenium.start();
selenium.open("http://www.google.cn/");//调用 selenium.open 方法,浏览器会打开相应的页面
selenium.type("q", "selenium");//使用 type 方法来给输入框输入文字
selenium.click("btnG");
selenium.waitForPageToLoad("30000");//等待页面载入
assertEquals("selenium - Google 搜索", selenium.getTitle());//看看新的页面标题是不是我们想要的。
} catch (Exception e) {
throw e;
} finally {
selenium.stop();
}
}
}

  六、如何选取元素

  selenium提供如下强大的定位元素的方法。

id=id
name=name
dom=javascriptExpression
xpath=xpathExpression
link=textPattern
css=cssSelectorSyntax

  1 通过ID,name选择元素  :  如 selenium.type("id=q","百度"); selenium.type("name=search","百度")

  2 link= 根据链接文字来操作:如 selenium.click("link=个人资料");

  3 根据XPath来选择元素  : XPath Checker

* xpath=//img[@alt='The image alt text']
* xpath=//table[@id='table1']//tr[4]/td[2]
* xpath=//a[contains(@href,'#id1')]
* xpath=//a[contains(@href,'#id1')]/@class
* xpath=(//table[@class='stylee'])//th[text()='theHeaderText']/../td
* xpath=//input[@name='name2' and @value='yes']
* xpath=//*[text()="right"]
如: selenium.type("xpath=//input[@name='user.email']", "xxx@123.com"); // 向input中type为text的栏位键入信息
selenium.check("xpath=//input[(@name='user.sex')and(@value='男')]");// 向input中type为radiod的 选取

  4 dom选择

* dom=document.forms['myForm'].myDropdown
* dom=document.images[56]
* dom=function foo() { return document.links[1]; }; foo();

  5 css选择器

  这个不常用,它可以支持css2, css3选择器

* css=a[href="#id3"]
* css=span#firstChild + span

  七、使用selenium 这个对象来进行测试

  1  获取标 : assertEquals("Insert title here", selenium.getTitle());

  2  判断页面是否存在一个user.email元素  :assertTrue(selenium.isElementPresent("xpath=//input[@name='user.email']"));

  3  得到文本框里的文字:   assertEquals(selenium.getValue("xpath=//input[@name='user.username']"),"xxxaas");

  4  测试check box  :    assertTrue(selenium.isChecked("xpath=//input[(@name='user.sex')and(@value='男')]"));

  5  点击提交按钮   : selenium.click("xpath=//input[@type='button']");

  6  等待页面载入   : selenium.waitForPageToLoad("2000");

  7  验证指定文本出现在提交给用户的页面上: assertTrue(selenium.isTextPresent("验证码输入有误,请核实后再输入"));

  8  判断下拉框里选择了哪个选项 :assertEquals(selenium.getSelectedIndex("xpath=//SELECT[@name='HATIMING']"), "1");

  9  如何测试一些错误消息的显示? assertTrue(selenium.getBodyText().indexOf("错误消息")>=0);

  getBodyText 返回的时浏览器页面上的文字,不回包含html 代码的,如果要显示html 代码,用下面这个:selenium.getHtmlSource();

  八、Firefox 的插件

  1 XPath Checker :可以用这个工具测试简化我们的xpath表达式

  2 Firebug

  3 Selenium IDE

  4 Execute JS

22/2<12
《2023软件测试行业现状调查报告》独家发布~

精彩评论

  • amnpt
    2015-5-27 14:40:10

    可不可以用java语言写的测试脚本来测试C++的网页系统呢?

  • wendy1102
    2012-2-13 17:38:29

    谢谢,正想学习

  • shyboy2
    2010-10-13 11:12:08

    谢谢啦   刚刚好写点有关这论文

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号