Sunday, 18 February 2018

How many ways to select an element in Selenium?


There are so many elements are available on a web page and to identify and locate an element on that page you need to mention the location of that particular element. 
  1. ID
  2. Class Name
  3. Tag Name
  4. Class
  5. Using XPath
  6. Using  CSS
  7. Using Javascript
  8. AutoIT(Alternative way)
Id:- Specify element by their id name in DOM
example:- driver,findElementById("Example") ;

Class Name:- Specify element by their class name in DOM
example:- driver.findElementByClassName("Example");

Tag Name:- Specify element by their tag name
example:- driver.findElementByTagName("Example");

XPath:-

   XPath uses 'path like' syntax to identify and navigate nodes in an XML document.
example: - //a[@id="example"]

CSS:-

CSS is the simplest form of an element location. It has much more benefits compared to XPath. The major one is the fastest UI interaction. When working on IE, most of the times XPath won't work.
example:- #example

Javascript:-

The fastest way to interact UI elements, implement with java executor. Due to JS asynchronous behavior, it is hard to manage.
example:- document.getElementById("example");

AutoIT:-

Used it when no methodology works. It is the simplest form of implementation to interact with UI.
Rather than DOm reading, it finds element image on a screen.  Highly unstable, a slight modification can break consistency.



How to modify host file for emulator and windows


In post, we know how to edit or update windows/emulator hosts file

Windows:-

Host File Location
Host file located in below under windows folder

C:\Windows\System32\drivers\etc

Block site Access from Hosts File
If you want to block host entries for the original host address used in place editable host addresses. Simply make an entry in your host file with below IP address and hostname

127.0.0.1       blocksite.com

Edit hosts File

Open the hosts file in notepad or any available text editor. and make the entries in the form of IP address followed by the hostname. 

Info If you editing for localhost, Used IP adress127.0.0.1 for localhost in place of local Ip address

Example:-

127.0.0.1       google.com

Emulator:-

Follow steps mentioned below to modify hosts file of emulator
  1. Open command promt Win+R and type cmd and click on Ok
  2. Switch to Android SDK's tools folder
    C:\Users\user\AppData\Local\Android\sdk\tools
  3. To get the list available(Created by you), emulator used below command
    emulator -list-avds
  4. Start the emulator with writable system image option using command.Emulator name spaces replaced by underscore
    emulator -avd emulator_name -writable-system
  5. Open new command prompt and switch to Android SDK's platform-tools folder
    C:\Users\user\AppData\Local\Android\sdk\platform-tools
  6.  Use the adb (Android Debug Bridge) tool to remount::
    a) adb root

    b) adb remount
  7. Run the following commands to update the hosts file. Use Ip address 10.0.2.2 for local host
    a) adb shell

    b)  cd /system/etc

    c)  echo "IP Address Hostname" >> hosts
  8. Check for the hosts file update bt command

    cat hosts



Wednesday, 30 August 2017

How to Install Ruby in Windows

Ruby Beginners has the biggest challenge that how to start with Programming language ruby, what will be the initial steps to learn, prerequisite and so on. To solve their huddle, I have written this post. The first thing which you need to do start with Ruby programming language is to install ruby in your system.

Steps to Download:-
  • Link to download the RubyInstaller for Windows
  • Choose your prefer ruby version available for both 32-bit and 64-bit version
  • Follow instruction to setup Ruby on your system
  • Open command and type “ruby -v”, If it provides you your ruby version
  • Congratulations, you have done your ruby system setup

Comment in case you are facing any challenges

Labels: , , ,

Saturday, 26 August 2017

Read and Write Properties File

In this post, I will tell you that how will you implement the utility to read and write the properties file. When you need to design an automation framework there are some utilities which you need to implement for reading and writing different types of files in your framework used for different purposes. The properties file is one of the best ways used to define the system configuration.

Code:-

public class PropFileHandler {

static Properties properties = new Properties();
public String readProperty(String property) {
InputStream inPropFile = null;
try {
inPropFile = new FileInputStream("Directory Path");
properties.load(inPropFile);
} catch (IOException e) {
System.out.println("There was Exception reading the Test data");
}
String value = properties.getProperty(property);
return value;

}


public static void writeProperty(String property, String value) {
try {
InputStream inPropFile = new FileInputStream("File Path");
properties.load(inPropFile);
inPropFile.close();
OutputStream outPropFile = new FileOutputStream("File Path");
properties.setProperty(property, value);
properties.store(outPropFile, null);
outPropFile.close();
} catch (IOException e) {
}
}

}

Thank you for reading my Post. Please like and comment if you like my post.

Labels: , , , ,

Code to take Screen Shots in Selenium WebDriver?


In this post, I will tell you that how would you capture the failure screenshots in Selenium WebDriver. A screenshot is the best way in order to tell the reason of automation script failure. By which a tester get the reason that on which page script gets failed and what was the reason. I am sharing the code of this with you.

Code:-


public class TakeScreenshot {


WebDriver driver;
String screenshotPath = "Your Path";


public void takeScreenshot() {
DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_hh_mm_a");
Date date = new Date();
String date_time = dateFormat.format(date);
File file = new File(System.getProperty("user.dir") + File.separator
+ screenshotPath + File.separator + date_time);
boolean exists = file.exists();
if (!exists) {
new File(System.getProperty("user.dir") + File.separator
+ screenshotPath + File.separator + date_time).mkdir();
}


File scrFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
try {
String saveImgFile = System.getProperty("user.dir")
+ File.separator + screenshotPath + File.separator
+ date_time+ File.separator + "screenshot.png";
Reporter.log("Save Image File Path : " + saveImgFile, true);
FileUtils.copyFile(scrFile, new File(saveImgFile));
} catch (IOException e) {
e.printStackTrace();
}
}


public void takeScreenShotOnException(ITestResult result) {
String takeScreenshot = screenshotPath ;
if (result.getStatus() == ITestResult.FAILURE) {
Reporter.log(
"FAILURE occurred at "
+ DateUtil.converttimestamp(System
.currentTimeMillis()), true);
if (takeScreenshot.equalsIgnoreCase("true")
|| takeScreenshot.equalsIgnoreCase("yes")) {
try {
if (driver != null) {
takeScreenshot();
}
} catch (Exception ex) {
Reporter.log("Driver is null while taking screen shot",
true);
}
}
}
}


}


Call this java class in Test class:-

Test Class:-

@AfterMethod
public void snapshot(ITestResult result){
test.takescreenshot.takeScreenShotOnException(result);
}


Labels: , ,

Tuesday, 22 August 2017

How to handle window Authentication Popup using Selenium WebDriver?

In this post, we learned how to handle the window authentications using selenium webDriver. Most of the companies are using this technique to enhance the security of its application. In that case, just before the launch of the application, an authentication pop up come to ask the authorized username and password. HTTP Authentication is the technique to resolve this problem using selenium.


How to implement HTTP Authentication using Selenium WebDriver


You need to append the username and password with the application URL to handle the window authentication pop up.




By using the above URL window authentication pop up will by pass and you can successfully automate that type of situation without any hurdle.


Imp:- You no need to make any changes in the system configuration other that IE browser.
For IE you need to make some changes to overcome the HTTP security of IE browser

Below are the steps to do so:-

For 32 bit OS:
Go to ‘HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE‘
Create two new DWORDS iexplore.exe and explorer.exe and make sure their values are 0.
For 64 bit OS:
Go to ‘HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE‘
Create two new DWORDS iexplore.exe and explorer.exe and make sure their values are 0.

Thanks, for reading my post. Please do comment in case of any query

Labels: ,

How to handle the windows, frames and pop up in Selenium WebDriver?

In this post, I will explain that how do you handle the multiple windows, frames, and pop-up of an application using Selenium WebDriver. You generally faced these types of a problem of frames, windows, and pop-up in an application. Different tabs are also treated as different windows. In software automation, you need to tell the Selenium WebDriver that where it will search for the element. In which frame window or pop up.


So to handle this situation selenium WebDriver has native methods. I have mentioned below different ways to handle the windows, frames, and pop-up.


How to handle Frames:-

1) Select frame by element
Ex:- driver.switchTo().frame(element);
2) Select frame by id:-
Ex:- driver.switchTo().frame(id);
3) Select frame by index:-
Ex:- driver.switchTo().frame(index);
How to handle Windows:-

1) Select Window From Index
Set<String> windows = driver.getWindowHandles();
System.out.println("Window Size::" + windows.size());
String wins[] = windows.toArray(new String[windows.size()]);
driver.switchTo().window(wins[WindowCountToSwitch]);


2) Close Window From Index
                      Set<String> windows = driver.getWindowHandles();
System.out.println("Window Size::" + windows.size());
String wins[] = windows.toArray(new String[windows.size()]);
driver.switchTo().window(wins[WindowCountToSwitch]);
driver.close();
Note:- WindowCountToSwitch is the count of the window to select or delete


How to handle the Pop Up:-


1)Handle pop Up:-
                       Alert alert = driver.switchTo().alert();
alert.accept();


2)Get Alert Text:-
                      Alert alert = driver.switchTo().alert();
            String message = alert.getText();


3)check for the visibility of Alert:-
                       WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.alertIsPresent());

Switch to Default/main window or frame:-
driver.switchTo().defaultContent();

Thank you for visiting my blog. Keep in touch.

Labels: , , , , , , , , ,