2015年8月3日 星期一

[ Java 套件 ] Selenium - Introduction

Source From Here 
Preface 
Selenium is an open-source tool that is used for test automation. It is licensed under Apache License 2.0. Selenium is a suite of tools that helps in automating only web applications. This tutorial will give you an in-depth understanding of Selenium and its related tools and their usage. 

Before proceeding with this tutorial, you should have a basic understanding of Java or any other object-oriented programming language. In addition, you should be well-versed with the fundamentals of testing concepts. 

Selenium is not just a single tool but a set of tools that helps testers to automate web-based applications more efficiently. Let us now understand each one of the tools available in the Selenium suite and their usage. 
 

Selenium IDE 
The Selenium-IDE (Integrated Development Environment) is an easy-to-use Firefox plug-in to develop Selenium test cases. It provides a Graphical User Interface for recording user actions using Firefox which is used to learn and use Selenium, but it can only be used with Firefox browser as other browsers are not supported. 

However, the recorded scripts can be converted into various programming languages supported by Selenium and the scripts can be executed on other browsers as well. The following table lists the sections that we are going to cover here. 
Download Selenium IDE 
This section deals with how to download and configure Selenium IDE.

Selenium IDE Features 
This section deals with the features available in Selenium IDE.

Creating Selenium IDE Tests 
This section deals with how to create IDE tests using recording feature.

Selenium IDE Script Debugging 
This section deals with debugging the Selenium IDE script.

Inserting Verification Points 
This section describes how to insert verification points in Selenium IDE.

Selenium Pattern Matching 
This section deals with how to work with regular expressions using IDE.

Selenium User Extensions 
The Java script that allows users to customize or add new functionality.

Different Browser Execution 
This section deals with how to execute Selenium IDE scripts on different browsers.

Environment Setup 
In order to develop Selenium RC or WebDriver scripts, users have to ensure that they have the initial configuration done. Setting up the environment involves the following steps. 
* Download and Install Java
* Download and Configure Eclipse
* Configure FireBug and FirePath
* Configure Selenium RC
* Configure Selenium WebDriver

Download and Install Java 
We need to have JDK (Java Development Kit) installed in order to work with Selenium WebDriver/Selenium. 
Install JDK in Windows
JDK Installation in Linux Platforms

Download and Configure Eclipse 
Prepare Eclipse IDE to develop Selenium WebDriver. (More

Configure FireBug and FirePath 
To work with Selenium RC or WebDriver, we need to locate elements based on their XPath or ID or name, etc. In order to locate an element, we need tools/plugins. (More

Configure Selenium RC 
Now let us look at how to configure Selenium Remote control. We will understand how to develop scripts with Selenium RC in later chapters, however for now, we will understand just the configuration part of it. 

Step 1 : Navigate to the Selenium downloads section http://www.seleniumhq.org/download/ and download Selenium Server by clicking on its version number as shown below. 
 

Step 2 : After downloading, we need to start the Selenium Server. To do so, open command prompt and navigate to the folder where the downloaded JAR file is kept as shown below. To start the server, use the command 'java -jar <<downloaded jar name >>' and if java JDK is installed properly, you would get a success message as shown below. Now we can start writing Selenium RC scripts. 
# ls -hl // Under folder with downloaded selenium jar file
total 31M
-rw-r--r--. 1 root root 31M Aug 3 12:38 selenium-server-standalone-2.47.1.jar

# java -jar selenium-server-standalone-2.47.1.jar // Start stand alone selenium server service
12:38:34.160 INFO - Launching a standalone Selenium Server
...
12:38:34.565 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
12:38:34.566 INFO - Selenium Server is up and running

Or if your want to use Docker to launch Selenium Service, below is an example: 

Configure Selenium WebDriver 
Now let us look at how to configure Selenium WebDriver. We will understand how to develop scripts with Selenium WebDriver in later chapters, however for now, we will understand just the configuration part of it. 

Step 1 : Navigate to the selenium downloads section http://www.seleniumhq.org/download/ and download Selenium WebDriver by clicking on its version number as shown below. 
 

Step 2 : The downloaded file is in Zipped format and one has to unzip the contents to map it to the project folder. 
Step 3 : Import the jar files into project 
 

Remote Control 
(Source) Selenium Remote Control (RC) allows us to write automated web application UI tests with the help of full power of programming languages such as Java, C#, Perl, Python and PHP to create more complex tests such as reading and writing files, querying a database, and emailing test results. 

Selenium RC Architecture 
Selenium RC works in such a way that the client libraries can communicate with the Selenium RC Server passing each Selenium command for execution. Then the server passes the Selenium command to the browser using Selenium-Core JavaScript commands. 

The browser executes the Selenium command using its JavaScript interpreter. 
 

Selenium RC comes in two parts. 
1. The Selenium Server launches and kills browsers. In addition to that, it interprets and executes the Selenese commands. It also acts as an HTTP proxy by intercepting and verifying HTTP messages passed between the browser and the application under test.

2. Client libraries that provide an interface between each one of the programming languages (Java, C#, Perl, Python and PHP) and the Selenium-RC Server.

RC Scripting 
Now let us write a sample script using Selenium Remote Control. Let us use http://www.calculator.net/ for understanding Selenium RC. We will create a sample RC case to open Google page and maximize the browser for testing Selenium Server Service availability. 

Step 1 : Start Selenium Remote Control (with the help of command prompt). 
Step 2 : Create Eclipse Project, Import Selenium Client Libraries and try below sample code: (Groovy
  1. package lab  
  2.   
  3. import com.thoughtworks.selenium.DefaultSelenium;  
  4. import com.thoughtworks.selenium.Selenium;  
  5.   
  6. // Instatiate the RC Server  
  7. Selenium selenium = new DefaultSelenium("192.168.140.141"4444 , "firefox""http://www.google.com.tw");  
  8. selenium.start();   // Start  
  9. selenium.open("/");  // Open the URL  
  10. selenium.windowMaximize();  
  11.   
  12. sleep(5)  
  13. selenium.stop();  
"192.168.140.141" is the IP address where running Selenium Server Service. 

Webdriver 
(Source) WebDriver is a tool for automating testing web applications. It is popularly known as Selenium 2.0. WebDriver uses a different underlying framework, while Selenium RC uses JavaScript Selenium-Core embedded within the browser which has got some limitations. WebDriver interacts directly with the browser without any intermediary, unlike Selenium RC that depends on a server. It is used in the following context: 
* Multi-browser testing including improved functionality for browsers which is not well-supported by Selenium RC (Selenium 1.0).
* Handling multiple frames, multiple browser windows, popups, and alerts.
* Complex page navigation.
* Advanced user navigation such as drag-and-drop.
* AJAX-based UI elements.

Architecture 
WebDriver is best explained with a simple architecture diagram as shown below. 
 

Selenium RC Vs WebDriver 
 

Scripting using WebDriver 
Now it is time to code. The following script is easier to understand, as it has comments embedded in it to explain the steps clearly. Please take a look at the chapter "Locators" to understand how to capture object properties. 
  1. package lab  
  2.   
  3. import org.openqa.selenium.remote.DesiredCapabilities  
  4. import org.openqa.selenium.remote.RemoteWebDriver  
  5. import java.util.concurrent.TimeUnit;  
  6.   
  7. import org.openqa.selenium.*;  
  8. import org.openqa.selenium.firefox.FirefoxDriver;  
  9.   
  10. DesiredCapabilities capability = DesiredCapabilities.firefox();  
  11. driver = new RemoteWebDriver(new URL("http://192.168.140.141:4444/wd/hub"), capability);  
  12.   
  13. //Puts an Implicit wait, Will wait for 10 seconds before throwing exception  
  14. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
  15.   
  16. //Launch website  
  17. driver.get("http://www.calculator.net/");  
  18.   
  19. //Maximize the browser  
  20. driver.manage().window().maximize();  
  21.   
  22. // Click on "7" from Calculators  
  23. driver.findElement(By.xpath(".//*[@id='sciout']/tbody/tr[2]/td[2]/div/div[1]/span[1]")).click();  
  24.   
  25. // Click on Plus from Calculators  
  26. driver.findElement(By.xpath(".//*[@id='sciout']/tbody/tr[2]/td[2]/div/div[1]/span[4]")).click();  
  27.   
  28. // Click on "3" from Calculators  
  29. driver.findElement(By.xpath(".//*[@id='sciout']/tbody/tr[2]/td[2]/div/div[3]/span[3]")).click();  
  30.   
  31. // Click on "=" from Calculators  
  32. driver.findElement(By.xpath(".//*[@id='sciout']/tbody/tr[2]/td[2]/div/div[5]/span[4]")).click();  
  33.   
  34. // Get the Result Text based on its id  
  35. String result = driver.findElement(By.id("sciOutPut")).getText();  
  36.   
  37. // Print a Log In message to the screen  
  38. System.out.println(" The Result is " + result);  
  39.   
  40. // Check if the browser is launching and opening google page now  
  41. printf("Sleep 5 sec...\n")  
  42. sleep(5)  
  43.   
  44. //Close the Browser  
  45. driver.close()  
  46. driver.quit()  
 

Most Used Commands 
The following table lists some of the most frequently used commands in WebDriver along with their syntax. 
 

Supplement 
Selenium wiki - Remote WebDriver 
Stackoverflow - Set chrome.prefs with python bindi...g for selenium in chromedriver 
Stackoverflow - RemoteWebDriver Chrome - start maximized 
Stackoverflow - How to build remote Webdriver for Chrome

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...