Selenium

Selenium 自动化测试指南
Selenium 是ThoughtWorks为Web应用开发的自动化测试工具,包括Selenium IDE、RC和Grid等组件。Selenium IDE是Firefox插件,用于录制和回放测试用例。Selenium 2.0环境搭建涉及Java Project配置和JUnit库的添加。Selenium RC自动化测试需要启动服务器,并在Eclipse中配置项目和测试类。

官方网站:http://www.openqa.org/

Selenium是ThoughtWorks 专门为 Web 应用而开发的自动化测试工具,适合进行功能测试、验收测试,核心-browser bot。

主要有以下四种类型:

1.      Selenium core: 整个测试机制的核心部分,即有assertion(断言)机制的test suiterunner。它由一些纯js代码组成,可以运行在不同操作系统的不同浏览器上。

2.      Selenium IDE: 一个firefox的plug-in,可以录制和回放并保存一些test cases, 可以生成一些简单的基于rc 模式的简单code.

3.      Selenium RC(remote control):=Selenium Server+ Selenium Libraries

一个代理与控制端,可代替Selenium core/ Selenium IDE的client端(相当于通过编程来实现一切),是支持多语言的

4.      Selenium Gird:允许同时在不同的环境中运行多个测试任务

 

网景提出的安全策略-同源策略:同源:域名、协议、端口相同。

Selenium Server 以代理的形式存在,通过修改WebSite 的源信息,从而达到欺骗浏览器的目。

Selenium IDE

http://www.cnblogs.com/hyddd/archive/2009/05/24/1487967.html

Selenium2.0环境搭建

1.右键 New一个Java Project 

2. 写入名称,点击Finish.

3.在src上右键,New一个Class,填写,确定

5. 在project名上右键,进入Properties

6. 点击Add Library

7. 选JUnit,然后next,选择JUnit4,Finish

8. OK

9. 新建文件夹,将selenium的jar放在lib下

10. 然后右键Add to build Path一下

11. 写入脚本

packagecom.selenium.test;

 

importorg.openqa.selenium.By;

importorg.openqa.selenium.WebDriver;

importorg.openqa.selenium.firefox.FirefoxDriver;

importorg.openqa.selenium.ie.InternetExplorerDriver;

 

importcom.thoughtworks.selenium.SeleneseTestCase;

 

public class Test {

      

       public static void main(String[] args) {

              // TODO Auto-generated method stub

              //创建一个WebDriver实例视力

              //WebDriver driver = newFirefoxDriver();

              WebDriver driver = newInternetExplorerDriver(); 

          driver.get("https://www.baidu.com");

          driver.findElement(By.id("kw")).sendKeys("123456");

          driver.findElement(By.id("su")).click();

          // 检查页面title

        System.out.println("Page title is:" + driver.getTitle());

        //关闭浏览器

        driver.quit();

 

       }

 

}

12. 点击运行

selenium RC自动化测试

1. 先去http://selenium-rc.openqa.org/download.jsp 下载selenium包。解压。 

2. 用命令行来到解压的文件夹下: \selenium-remote-control-0.9.2\selenium-server-0.9.2

3. 运行: java-jar selenium-server.jar 启动seleniumserver

4. 在Eclipse创建一个项目,在项目的build path里面加上junit.jar和selenium-java-client-driver.jar(这个在刚解压的包里面)

5. 在项目里面新建一个junit文件,比如说openQA这个网站上的GoolgeTest文件

6. 在红色波浪线的那些地方,比如Selenium, DefaultSelenium上面‘Ctrl+1’来增加相应要import的类

7. 然后在Eclipse里运行 “Run As -> unit Test”即可看到自动化的范例

最后粘贴一下那个测试程序

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

import junit.framework.TestCase;

public class GoogleTest extends TestCase {
    private Selenium selenium;

    public void setUp() throws Exception {
        String url ="http://www.google.com";
        selenium = newDefaultSelenium("localhost", 4444, "*firefox", url); //4444 is default server port

   selenium.start();      
    }

    protected void tearDown() throws Exception {
        selenium.stop();
    }

    public void testGoogle() throws Throwable {
       selenium.open("http://www.google.com/webhp?hl=en");

        assertEquals("Google", selenium.getTitle());
        selenium.type("q","Selenium OpenQA");
        assertEquals("SeleniumOpenQA", selenium.getValue("q"));
        selenium.click("btnG");
       selenium.waitForPageToLoad("5000");
        assertEquals("Selenium OpenQA -Google Search", selenium.getTitle());
    }
}

### Selenium 使用指南 #### Selenium 的发展历史与主要组成部分 Selenium 3于2009年发布,这一版本去除了Selenium RC,其核心组件主要包括Selenium WebDriver 和 Selenium Grid[^1]。其中,Selenium WebDriver 是我们日常开发中最常用的模块,而 Selenium Grid 则用于支持分布式环境下的自动化测试。 --- #### Selenium 的安装方法 为了在 Python 中使用 Selenium 进行浏览器操作,需先完成以下准备工作: 1. **安装 Selenium 库** 可通过 `pip` 命令来安装 Selenium: ```bash pip install selenium ``` 2. **下载对应的浏览器驱动程序** 不同的浏览器需要不同的驱动程序。例如,如果使用的是 Microsoft Edge 浏览器,则需要在其官方页面上查找并下载与当前浏览器版本匹配的驱动程序[^3]。具体步骤如下: - 打开浏览器设置界面,确认当前浏览器的版本号。 - 访问 [Microsoft Edge WebDriver](https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/) 页面,选择与之相匹配的驱动文件进行下载。 - 将下载好的驱动放置到系统的 PATH 路径下或者指定路径供脚本调用。 --- #### Selenium 的基本使用流程 以下是利用 Selenium 实现简单网页浏览的一个示例代码片段: ```python from selenium import webdriver from selenium.webdriver.edge.service import Service as EdgeService # 设置Edge驱动服务对象 service = EdgeService(executable_path="path/to/msedgedriver") # 初始化WebDriver实例 driver = webdriver.Edge(service=service) try: # 打开目标网站 driver.get("http://www.example.com") # 获取页面标题 title = driver.title # 输出页面标题 print(f"Page Title is {title}") finally: # 关闭浏览器窗口 driver.quit() ``` 上述代码展示了如何加载一个简单的网页,并获取该网页的标题信息[^2]。 --- #### Selenium 的应用场景 Selenium 是一款非常强大的工具,广泛应用于以下几个领域: - 自动化测试:验证 Web 应用的功能是否正常运行。 - 数据抓取:模拟真实用户的交互行为从而提取所需的数据。 - UI/UX 验证:检查前端布局以及用户体验是否存在异常情况。 --- 问题
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值