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