平平庸庸

selenium学习笔记

上一篇 / 下一篇  2009-04-27 11:31:08 / 个人分类:随便写写

声明:为节省您的时间,请勿阅读这篇日志。这篇对你没有任何意思。纯粹自己的笔记。很混乱。

Selenium RC :http://selenium-rc.openqa.org/

命令行的方式用如下的命令来启动你的Selenium Serverjava -jar selenium-server.jar -interactive

Entering interactive mode... type Selenium commands here (e.g: cmd=open&1=http://www.yahoo.com)

打开浏览器:cmd=getNewBrowserSession&1=*iexplore&2=http://www.google.com

进入google
cmd=open&1=http://www.google.com/webhp&sessionId=260113

搜索框输入helloworld

cmd=type&1=q&2=hello world&sessionId=260113

点击按钮

cmd=click&1=btnG&sessionId=260113


找标题,如下:

cmd=getTitle&sessionId=260113

测试结束

cmd=testComplete&sessionId=260113

输入这些命令后,你的浏览器窗口将会自动关闭,想要关掉Selenium服务器,输入“quit”或者只要按下“Ctrl+C”。

---------

The Same Origin Policy 同源策略

As you were running your tests, you may have noticed that your browser started at the following URL:

http://www.google.com/selenium-server/RemoteRunner.html?sessionId=260113

That's a rather unusual URL, because, of course, there is no such file available on www.google.com. If you open up your browser manually and browse to that URL, you'll get a 404 error. What's going on?

The Selenium Server is attempting to circumvent a very difficult problem in JavaScript. automated testing: normally, JavaScript. you write yourself can't be run on google.com, due to a policy known as thesame origin policy. (That write-up is from the Mozilla website, but all modern JavaScript. browsers enforce this policy.) The same origin policy states that JavaScript. is only allowed to read/modify HTML from thesame originas its source.

That policy makes a lot of sense. Let's say you've got your browser window pointing at, for example, your bank's website, but you also have another webpage open pointing to someone's blog. JavaScript. is allowed toreadvalues from web pages, as well aschangedata appearing on webpages you've loaded. If not for the same origin policy, a malicious blogger could read your bank data, or worse, rewrite your bank page to make you think it was saying something else. The blogger could use JavaScript. to trick you into giving him sensitive information.

Despite the soundness of the policy, it creates a problem for Selenium automated tests. If you write a .js file designed to test google.com, the same origin policy denies you the right to just run that .js file with google.com; instead, you'd have to somehow install that .js fileon google.comin order to write automated tests against it. In the case of google.com we don't have the right to do this; but even if we did, it would be a hassle to do so.

That's where the Selenium Server comes in. The Selenium Server is acting as aclient-configured proxyfor the browser that you automatically started with "getNewBrowserSession". Specifically, it configures your browser to use the Selenium Server as a proxy in its browser preferences.

A proxy normallyfetchesHTML pages on your behalf; if a page can't be found, it honestly reports that the page wasn't there. But the Selenium Server is a very different kind of proxy; when the browser requests a page through the proxy that contains "/selenium-server/" in its URL, the Selenium Server doesn't simply fetch the page from the remote server, but instead automatically returns its own page instead. That makes the browser think that the remote server served up our JS, which allows us to "inject" arbitrary JavaScript. into google.com without modifying google.com directly.

However, the solution isn't perfect. Try running your tests like this:

cmd=getNewBrowserSession&1=*iexplore&2=http://www.yahoo.com
cmd=open&1=http://www.google.com&sessionId=260113
cmd=type&1=q&2=hello world&sessionId=260113

In this case, the "start URL" was yahoo.com, so the browser opened to

http://www.yahoo.com/selenium-server/RemoteRunner.html?sessionId=260113

and then we tried to open up google.com (which did work...) and type "Hello World" into the search box. But yahoo.com doesn't have the right to modify google.com, so that operation fails.

All this means is that you have to choose your browser's start URL wisely: if you open up the browser to yahoo.com, you can't use it to test google.com, and vice versa.

However, in some cases, you may berequiredto test more than one domain at once. The most common case is when you need to test both http://blah.comandhttps://blah.com. "http:" and "https:" are considered different "origins" from JavaScript. perspective, so tests running on one can't run on the other. In that case, for now, you'll have to use either the*chrome, or the*iehtabrowser launchers which support running in multiple domains. (Also be sure to read theHTTPSsection of this documentation for more details about HTTPS support.)

------

https

Using the CyberVillains CA manually

Did I mention thatusing the CyberVillains CA is inherently dangerous? Using it manually ismoredangerous, because it introduces the risk that you'll forget to uninstall the CyberVillains CA when you're done using Selenium RC. You MUST manually uninstall the CyberVillains CA after you're finished using Selenium RC.

Windows/IE: Look for "cyberVillainsCA.cer" in the "server" folder in the selenium-remote-control zip, double-click on it, and press the "Install Certificate" button. Then click "Next" a few times, and finally "Finish" to install the CA certificate. To uninstall the CA certificate, use the "Internet Options" Control Panel, click on the "Content" tab, and click on the "Certificates" button. Finally, under the "Trusted Root Certification Authorities" tab, scroll down to CyberVillains and click on the "Remove" button to remove the cert.


 -----

跨域小结

使用如下方式开始.同时这样也无需为ssl添加证书

cmd=getNewBrowserSession&1=*iehta&2=https://www.99bill.com

-----

nice article about testng and selenium

http://www.ibm.com/developerworks/cn/java/j-cq04037/index.html

 

关于rc编译的文章

http://wiki.openqa.org/display/SRC/Developer's+Guide

 selenium文档

 http://code.google.com/p/selenium/


TAG: Selenium selenium

 

评分:0

我来说两句

Open Toolbar