Selenium 基础知识和命令

本文介绍Selenium工具的基础知识及其在Web应用程序测试中的应用。Selenium可用于测试浏览器兼容性、系统功能及支持多种编程语言的测试脚本。文章还详细介绍了Selenium IDE、Selenium Remote Control的使用方法以及命令行参数配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在公司遇到一个项目,是使用Selenium,是写脚本来运行web程序。一下是看书和查资料总结的内容。


1:基础知识 

 一个用于Web应用程序测试的工具,Selenium 测试直接运行在浏览器中,就像真正的用户在操作一样。这个工具的主要功能包括:测试与浏览器的兼容性--测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能--创建衰退测试检验软件功能和用户需求。 支持自动录制动作,和自动生成。Net、Java、Perl等不同语言的测试脚本。Selenium 是 ThoughtWorks 专门为 Web 应用程序编写的一个验收测试工具。

 通过编写模仿用户操作的 Selenium 测试脚本,可以从终端用户的角度来测试应用程序。

 能把Selenium RC脚本和JUnit单元测试结合起来,既能涵盖功能测试,又能涵盖数据或后台Java类测试,从而构成一个完整的Web应该测试解决方案。

 

Selenium IDE:  一个Firefox插件,可以录制用户的基本操作,生成测试用例。随后可以运行这些测试用例在浏览器里回放,可将测试用例转换为其他语言的自动化脚本。
Selenium Remote Control (RC) : 编写测试脚本。

 

2: Selenium command: 

java -jar selenium-server.jar [-interactive] [options]
 
 -port <nnnn>: selenium服务器使用的端口号(默认 4444)
 
 -timeout <nnnn>: 我们放弃前(超时)所等待的秒数

 -interactive: 进入交互模式

 -multiWindow: 进入被测试网站都在单独窗口打开的模式,并且selenium支持frame

 -forcedBrowserMode <browser>: 设置浏览器模式(例如,所有的会话都使用"*iexplore",不管给getNewBrowserSession传递什么参数)

 -userExtensions <file>: 指定一个被载入到selenium的JavaScript文件

 -browserSessionReuse: 停止在测试间重新初始化和替换浏览器。

 -alwaysProxy: 默认情况下,我们尽量少的进行代理;设置这个标志将会强制所有的浏览器通讯都通过代理

 -firefoxProfileTemplate <dir>: 一般情况,我们在每次启动之前都生成一个干净的Firefox设置。您可以指定一个目录来让我们将您的设置拷贝过来,代替我们生成的。

 -debug: 进入debug模式,会有更多的跟踪调试信息

 -htmlSuite <browser> <startURL> <suiteFile> <resultFile>: 使用指定的浏览器(例如"*firefox")在指定的URL(例如"http://www.google.com"),运行一个单独的HTML Selenese (Selenium Core)测试套件然后立即退出。您需要指定HTML测试套件的绝对路径还有我们将会生成的HTML测试结果文件的路径。

 -proxyInjectionMode: 进入代理注入模式,这个模式中selenium服务器作为进入测试程序的所有内容的代理服务器。在这个模式下,可以跨多个域访问,并且还支持如下附加参数:
 -dontInjectRegex <regex>: 附加的正则表达式,代理注入模式能够使用它决定是否进行注入
 -userJsInjection <file>: 指定一个JavaScript文件,将它注入到所有页面中
 -userContentTransformation <regex> <replacement>: 一个正则表达式,对所有被测HTML内容进行匹配;第二个string将会对替换所有匹配的内容。这个标志能够使用多次。一个简单的适合使用这个参数的例子:如果你添加"-userContentTransformation https http"那么测试应用程序的HTML中的所有"https"字符串都会被替换为"http"。

 -firefoxProfileTemplate <dir>: 一般情况,我们在每次启动之前都生成一个干净的Firefox设置。您可以指定一个目录来让我们将您的设置拷贝过来,代替我们生成的。

通常在开发过程中使用的https的安全证书都是不受信任的,对于这类证书我们通常会把它添加到firefox的例外列表中,但Selenium在启动firefox进行测试时会自动创建一个新的profile,这二个profile与我们平时使用的firefox进行web浏览时的profile是不同的,因此对安全证书的例外设置是无效的。解决的方法是为firefox创建一个开发用的profile,然后在启动RC server时使用"-firefoxProfileTemplate"参数指定这个profile的路径。

How to create Firefox profile for your Selenium?

    Make sure all your firefox instance are closed
    Click Start>Run
    Type “firefox.exe -ProfileManager -no-remote”
    Select “Create Profile” (i.e. selenium)
    Click “Next”
    Enter new profile name
    Select a directory folder to store your new profile
    Click “Finish”
    Select “Don’t ask at startup”
    Click “Start Firefox” and configure settings based on suggestion below***
    Set Profile back to “default” (enable you to use your previous settings on your browser)
    Add -firefoxProfileTemplate command line option as you start the Selenium Server

java -jar selenium-server.jar -firefoxProfileTemplate “<Selenium Profile Directory>”

*** Suggested settings for your Selenium Profile

    From “View\Toolbars” tab, uncheck “Bookmarks Toolbar”
    Right click from toolbar and click “Customize”
    Remove “Google search” by dragging it to the “Customize Toolbar” window
    From the “Customize Toolbar” window, click “Use Small Icons” check box then hit “Done”
    Click “Tools\Options” then set the following:
    a. “Main” tab
    - set Home Page to “about:blank”
    - uncheck “Show the Downloads..” option
    b. “Tabs” tab
    - Select “a new window” for new pages
    - Uncheck all warning options
    c. “Content” tab
    - uncheck “Block pop-up” windows option
    d. “Privacy” tab
    - uncheck all “History” options
    e. “Security” tab
    - uncheck all “Security” options
    - click “Settings” and uncheck all warning options
    f. “Advanced” tab
    - Uncheck “autoscrolling” option from “General” tab
    - uncheck “warn me …” and “Ssearch Engines”option from “Update” tab
    From “Tools\Add-ons” install the following:
    - Firebug: allows you to edit, debug, and monitor CSS, HTML, and JavaScript on your application under test
    - Selenium IDE: allows you to record, edit, and debug Selenium tests
    - ScreenGrab: saves entire webpages as images.
    From the address bar type “about:config” and add the following by right-click anywhere on the page and selecting “new”
    - extensions.update.notifyUser (type=boolean; value=false)
    - extensions.newAddons (type=boolean; value=false)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值