Tuesday, September 27, 2016

How to enter text in text field in selenium webdriver


 If you are new to Selenium Webdriver, please follow the steps in below link to successfully setup Selenium Webdriver on your machine.
If you have already setup Selenium Webdriver, please follow the steps to click on a link or button.
1. Launch webpage where your link or button is available.
2. Open firepath tab in firebug console
3. Click on inspect mouse icon displayed on top left in firebug console
4. Now click on the text field you want to enter text using Selenium Webdriver
5. Once you have clicked on the text field, the Xpath text field in firepath tab will display a xpath value
6. Copy the xpath
7. Now add this xpath and add it into your code as displayed in below example.

Let's do it with an example to enter a new comment in this post

package <Your package name here>;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class <YourClassNameHere>{
public static void main(String[] args) throws InterruptedException 
{
WebDriver driver = new FirefoxDriver();
driver.get("http://sushilsingh7688.blogspot.in/2016/09/how-to-enter-text-in-text-field-in.html");
driver.findElement(By.xpath(".//*[@id='commentBodyField']")).sendKeys("I have successfully entered a text in comments section");  // Using sendKeys method to enter text
driver.findElement(By.xpath(".//*[@id='postCommentSubmit']")).click();  // clicking on Publish button
Thread.sleep(5000);  // Waiting for 5 seconds to see entered comment in section
driver.quit();
}
}

Note: 
1. To run above code in eclipse, you must have firefox version latest-2 installed on your machine and selenium-java dependency added in your pom.xml file.
2. This xpath is very elementary and can fail even if there are minor changes in webpage code. when learning more in selenium webdriver, we can create customized xpath which are smaller and more reliable to success.

If you still face any issue in running the code, write to me in comments section.

How to click on a link or button in selenium webdriver


If you are new to Selenium Webdriver, please follow the steps in below link to successfully setup Selenium Webdriver on your machine.


If you have already setup Selenium Webdriver, please follow the steps to click on a link or button.

1. Launch webpage where your link or button is available.
2. Open firepath tab in firebug console
3. Click on inspect mouse icon displayed on top left in firebug console
4. Now click on the link or button you want to click using Selenium Webdriver
5. Once you have clicked on the link or button, the Xpath text field in firepath tab will display a xpath value
6. Copy the xpath
7. Now add this xpath and add it into your code as displayed below

Let's do it with an example to click on google plus profile pic in my blog

package <Your package name here>;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class <YourClassNameHere>{

public static void main(String[] args) throws InterruptedException 
{
WebDriver driver = new FirefoxDriver();
driver.get("http://sushilsingh7688.blogspot.in/");
driver.findElement(By.xpath(".//*[@id='Profile1']/div/a[1]/img")).click();
Thread.sleep(5000);
driver.quit();
}
}

Note: 
1. To run above code in eclipse, you must have firefox version latest-2 installed on your machine and selenium-java dependency added in your pom.xml file.
2. This xpath is very elementary and can fail even if there are minor changes in webpage code. when learning more in selenium webdriver, we can create customized xpath which are smaller and more reliable to success.

If you still face any issue in running the code, write to me in comments section.

Saturday, September 24, 2016

How to upload a file via browse in selenium webdriver

As you know that Selenium webdriver does not support uploading of files via browse button, so we need some alternate options for it.
I am providing an alternate solution which will use robot class. Robot class is a class which works as if a user is manually clicking keyboard keys or using a mouse.

Packages to Import:
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;

import java.awt.event.KeyEvent;

Step 1: Click on browse button

Step 2: Save complete file location path to a variable. Use double forward slashes instead of single as single slashes are not supported in java.
String filepath = "C:\\Users\\sushilsingh\\image.jpg"

Step 3: Copy file location path from clipboard in a variable
public static void setClipboardData(String filepath)
{
//StringSelection is a class that can be used for copy and paste operations. Below code will copy the text from clipboard and save it in a variable.
StringSelection stringSelection = new StringSelection(filepath);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);

}

Step 4: Declare robot class
robot = new Robot();

Step 5: Now perform paste operation using robot class. We will perform ctrl + v operation using robot class. Robot needs both press and release actions for an operation to perform.
robot.keyPress(KeyEvent.VK_CONTROL);  // Pressing control (ctrl ) key
robot.keyPress(KeyEvent.VK_V);  // Pressing V key 
robot.keyRelease(KeyEvent.VK_V);  // Release V key
robot.keyRelease(KeyEvent.VK_CONTROL);  // Release control ( ctrl ) key
robot.keyPress(KeyEvent.VK_ENTER);  // Press Enter key to click on open button in browse pop-up 

robot.keyRelease(KeyEvent.VK_ENTER);  // Release Enter key to click on open button in browse pop-up

The above steps will perform a file upload operation via browse button.

If you still face any issue, please write in comments section

How to work with drop down button in Selenium webdriver



There are multiple methods to select an option from drop-down. We can select the value from the drop down by using below 3 methods.


1. selectByValue  - Select by value of option in drop down
2. selectByVisibleText - Select by the text displayed in drop down
3. selectByIndex  - Select by index of option in drop down

Refer to below screenshot for an example of HTML code of drop down in a website


So as per HTML code, if we need to select 'Male' from drop-down, we have 3 options to select it.
1. By value - 1 ( As it is defined in HTML code)
2. By Text - Male (It is case sensitive. so follow it)
3. By index - 1 (1st option in drop down always has index = 0)

Now comes the webdriver code:
1. Select gender= new Select(driver.findElement(By.xpath(enter xpath of drop-down here)));
2. gender.selectByValue("1"); // Selecting by value
or
2. gender.selectByVisibleText("Male"); // Selecting by visible text in drop-down
or
2. gender.selectByIndex(1); // Selecting by index

The above steps will select a value from drop down.
If you still face any issue, please write in comments section.

How to switch between frames in selenium webdriver


Sometimes it happens that there is a link or a button which is located inside a frame in
website code. When selenium webdriver beginners try to click on it, they fail and had
hard time to understand and investigate why is button not clickable even if the locator
path is correct.
So, if you too are facing the same issue, you need to validate if your object is within a
frame. Follow the below steps:
1. Launch webpage in firefox browser which has the object
2. Install firebug and firepath add-ons . These add-ons are very helpful in selenium
webdriver
3. Click on F12 key in keyboad to launch the firebug panel
4. Right click on the object which is within a frame and select "inspect in firepath"
5. Now verify the window name in top left in firebug panel ( See below screenshot )

6. If this option says anything other than 'Top Window' then your object is surely
within a frame.

So, to click an element within a frame, you first need to switch to frame. Selenium
webdriver provides method driver.switchTo().frame().
You can switch to a frame by it's name, number or ID. Just locate you frame in
console and enter the name/number/id in brackets.

The above steps will help you to click on an element which is located within a frame.
If you still face any issue, please write in comments section.

How to create firefox profile in selenium webdriver


When we run selenium webdriver using firefox browser, the launched session is free of cookies and no firefox browser history is remembered. When clean browser is launched everytime, tests tend to become slower. So to make our tests faster, we can set firefox profile to run our tests faster. By default, the firefox profile is named as "default". We can view/create/rename/delete firefox profiles using firefox -p command in run window in windows OS. Make sure no firefox browser session is open when you run this command.

Generally defining the default profile works, but if you want to use any other profile, you can set it. When defining the new webdriver use the below code:

ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("enter your firefox profile name here within double quotes");
WebDriver driver = new FirefoxDriver(profile);

The above steps will define a new firefox profile for your selenium tests with all cookies remembered.
If you still face any issue, please write in comments section.

Friday, September 23, 2016

Starting with Selenium Webdriver

If you are new to Selenium and want to learn Selenium Automation, follow the below steps to setup selenium and start automation.

1. Download latest version of Eclipse from https://www.eclipse.org/neon/
2. Unzip the zipped file after download
3. Install latest version of jdk and jre on your machine from http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
4. Install firefox browser (latest version-2). I recommend to install a little previous version as it will be more compatible with selenium than the current version. http://filehippo.com/download_firefox/history/
5. Launch eclipse exe
6. It will ask you to create a workspace. Workspace will be a folder where all your projects will be placed
7. Wait for eclipse to launch. It may take a minute or two
8. If eclipse is properly launched, you will see the below eclipse homepage.
9. Now click on file in menu options > New > Other > Maven > Maven Project > Click Next > Again Next > Again Next > Enter a group id (Enter any word) > Enter Artifact id ( This will be your project name ) > Click finish
10. Clicking on finish will start downloading selenium backend files in your machine. You can see this process on bottom right of your eclipse window. Wait for it to finish.
11. Now expand to view your project files by clicking on > icon before your project name
12. You will see that there is file named pom.xml
13. Double click on pom.xml file
14. You will see that there are some tabs like overview, dependencies etc click on last tab pom.xml
15. Now open url  https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
16. Click on first non beta version selenium java version from version column
17. You will see selenium-java dependency displayed after click which looks like below


18. Copy full selenium-java dependency
19. Paste it into your pom.xml file under <dependencies> section. Save and close pom.xml file
20 This process will download selenium-java supported files in your machine.Download progress can be seen on bottom right of eclipse window.
21. Now you are ready to start writing your first code for selenium automation in eclipse.

Follow my other posts on how to start easy automation tasks like clicking a link, selecting radio button, check box etc.

If you still face any issue, please write in comments section.