Introduction and installation of selenium ide

Source: Internet
Author: User
Tags selenium grid

Selenium is a useful automated Web Testing Tool. Aaron used selenium a long time ago, but only used the APIS provided by Aaron to write test code. He also posted a simple code example on his blog. Recently, several bloggers have asked me questions about selenium recording via MSN. Unfortunately, I have never used selenium before. In view of this, the script was finally run after a while last night. Aaron hopes to help those bloggers and others.

Introduction and installation of selenium ide

Selenium recording is implemented by selenium IDE. According to the current introduction on the official website:

Selenium ideIs a Firefox add-on that records clicks, typing, and other actions to make a test, which you can play back in the browser.

Selenium ide helps us record clicks, inputs, and other behaviors and supports playback in browsers.

Of course, the reader may have noticed that selenium IDE is a Firefox plug-in, so the current version of selenium () does not support recording of IE, of course, this does not have a great impact on the use of scripts-unless you want to directly use the script after recording, you will be disappointed that the current version of selenium is not as powerful as that.

Selenium ide installation is very simple, if you use Firefox 2.0, you can directly put the files downloaded from the official website (such as selenium-ide-1.0-beta-2.xpi) in the extensions folder under the Firefox installation path (default installation path C: \ Program Files \ Mozilla Firefox \ extensions, and the default path of firefox1.5 is under c: \ Program Files \ Firefox plus \ app \ Firefox \ extensions), and close the Firefox window that is currently open, re-open to perform automatic installation. No installation failure occurred during use of Aaron.

If you are using Firefox 3.0, congratulations! You have won the prize: You will fail, and the reason is very simple. The maximum version supported by selenium ide has not reached the height of our 3.0. (Correction: thanks to the reminder on the first floor, Aaron has verified that selenium ide of version 1.0 and beta 2 can be successfully installed in firefox3. We sincerely apologize for misleading Aaron)

In addition, it should be noted that recording with the latest selenium ide may cause recording to fail (selenium-ide-1.0-beta-1.xpi, selenium-ide-1.0-beta-2.xpi version Errors During Aaron's use, prompting: table view is not available in this format). This bug has been submitted to the selenium Forum. The submitter uses Win XP, while Aaron uses Win 2003, so in order to avoid wasting time-despite being a waste of possibilities, Aaron recommends temporarily using the 0.8.7 version I tested.

Statement: As the version changes, some of the content in this article may be different from the use of the latest version. Therefore, the content introduced by Aaron is applicable to selenium versions earlier:

Project

Release Date

Version

Selenium Core

Jan 12,200 9

1.0 Beta 2

Selenium ide

June 3

2008 1.0 Beta 2

Selenium RC

Jan 12,200 9

1.0 Beta 2

Selenium Grid

Nov 30,200 8

1.0.3

Selenium ide script recording

Now that the installation is complete, we can use ide to record it directly. Start the Firefox browser and click the "Tools" menu in the Firefox menu bar. We can see that selenium IDE is its sub-menu:

Click the selenium ide item. The selenium ide window is displayed:

Then we can use selenium IDE for recording. As for the detailed recording process, I will not introduce it in detail, but I 'd like to remind you that, we can use the "format" sub-menu in the menu bar "options" of selenium ide to convert the script to the language type required by each other.

In the next part of the article, Aaron uses C # As the example language.

Edit selenium ide script

The script workflow recorded by Aaron is: open the Firefox browser-> open the Google homepage-> enter "google" in the Google search box-> left-click "Google Search"-> select "Search for images and News" on the new page to verify:

The final C # script is as follows:

Using system;
Using system. text;
Using system. Text. regularexpressions;
Using system. Threading;
Using nunit. Framework;
Using selenium;

Namespace seleniumtests
{
[Testfixture]
Public class newtest
{
Private iselenium selenium;
Private stringbuilder verificationerrors;

[Setup]
Public void setuptest ()
{
Selenium = new defaselselenium ("localhost", 4444, "* Firefox", "http://www.google.com ");
Selenium. Start ();
Verificationerrors = new stringbuilder ();
}

[Teardown]
Public void teardowntest ()
{
Try
{
Selenium. Stop ();
}
Catch (exception)
{
// Ignore errors if unable to close the browser
}
Assert. areequal ("", verificationerrors. tostring ());
}

[Test]
Public void thenewtest ()
{
Selenium. Open ("http://www.google.cn /");
Assert. areequal ("google", selenium. gettitle ());
Selenium. Type ("Q", "Google ");
Selenium. Click ("btng ");
Selenium. waitforpagetoload ("30000 ");
Assert. areequal ("Google-Google Search", selenium. gettitle ());
Try
{
Assert. istrue (selenium. istextpresent ("image, news search "));
}
Catch (assertionexception E)
{
Verificationerrors. append (E. Message );
}
}
}
}

 

Generally, these recorded scripts run successfully again in selenium IDE, but what if we take the script out directly in our own ide?

To make it easier to edit the recorded script, copy this code to vs: Create a class library project testseleniumsimple, and overwrite the content in the class1.cs file of the class library project with the recorded script. Compile our class library project testseleniumsimple. Sorry, we saw it.

Don't be surprised, because we can see it by taking a closer look at the code. The original recorded script references some content:

Using nunit. Framework;

Using selenium;

For the first one, we need to install nunitframework. This can be downloaded from the nunit official website. If you download selenium-RC at the same time, you can find it in the \ selenium-RC \ selenium-remote-control-1.0-beta-2-dist \ selenium-remote-control-1.0-beta-2 \ selenium-dotnet-client-driver-1.0-beta-2 folder, you can also find the thoughtworks we need for "Using Selenium. selenium. core. DLL, add references to the two DLL, and then compile. This can be generated successfully.

Play back the script in the test framework

It is a pity that the compiled scripts cannot be run. After compiling the script, nunit cannot be used. I don't know whether it is accidental or necessary. Fortunately, Aaron's script is recorded in the virtual machine, so Aaron takes the compiled script directly to the physical machine for running. Open nunit (Aaron is using the NUnit-2.4.3-net-2.0 version), then import testseleniumsimple. dll (testseleniumsimple class library product), click run, and then error:

It turned out to be a server. Do you still remember that we just referenced two DLL files in selenium-RC? RC in selenium RC indicates the remote client. Since there is a client, there must be a server. In fact, there is such a selenium-server, a guy who starts a web browser. Find the server subdirectory A in the selenium-RC directory:

\ Selenium-RC \ selenium-remote-control-1.0-beta-2-dist \ selenium-remote-control-1.0-beta-2 \ selenium-server-1.0-beta-2

There is a selenium-server.jar file in it, and we need to start this server under the command line.

Locate subdirectory A in the command line, and then enter:

Java-jar selenium-server.jar

At this time, we can see that we successfully started selenium server, and tried to run the test script that failed just now, and finally green:

We can also note that the command line tool also has the running information:

For example, we can see in the last four lines:

13:04:57. 406 Info-command request: istextpresent [image, news search,] on session

F68a3d7d0d7b4de8bfdb95ae1c553e6b

And so on.

Summary

In fact, we can also directly use the test edition component in vsts to run our test script without using nunit. In order to make the script more stable or faster, you can also edit the script to process the time. We can also edit the script to add more assertions to ensure that the page to be tested displays what we want or what we don't want.

For the selenium ide recording script, Aaron will summarize the following:

1. Use Firefox

2. Add references to nunit. Framework. dll and thoughtworks. Selenium. Core. DLL before compilation.

3. Remember to start selenium Server

4. To improve the script quality to meet testing stability and other requirements, edit the recorded script.

Of course, in the process of using selenium, we will encounter other strange problems and cause the script to have problems. With limited space and time constraints, Aaron will not proceed with the discussion. In addition, we can also write some programs to help us better use selenium (such as automatically starting selenium-server, and automatically running scripts). Aaron also leaves this content for everyone to practice ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.