Wednesday, November 27, 2013

Selenium : Creating CSS Selector using Xpath (CSS vs Xpath)





Creating CSS selector using Xpaths


Table 1: CSS vs. Xpath


Goal
CSS 3
XPath
All Elements
*
//*
All P Elements
p
//p
All Child Elements
p > *
//p/*
Element By ID
#foo
//*[@id='foo']
Element By Class
.foo
//*[contains(@class,'foo')] 1
Element With Attribute
*[title]
//*[@title]
First Child of All P
p > *:first-child
//p/*[0]
All P with an A child
Not possible
//p[a]
Next Element
p + *
//p/following-sibling::*[0]



Table 2: Table 1: CSS vs. Xpath


XPath
CSS equiv
Explanation
//div
div
Find elements of a particular type
id(‘bob’)
or
//*[@id='bob']
#bob
Find elements with a specific ID
//div[@id='bob']
div#bob
Find an elements of a particular type with a specific ID
//*[@class='bob']
.bob
Find elements with a specific class
//div[@class='bob']
div.bob
Find elements of a particular type with a specific class
//div//a
div a
Find descendant elements of a particular type
//div/a
div > a
Find direct child elements of a particular type
//a[@title]
a[title]
Find elements of a particular type that have a specific attribute
//a[@title='bob']
a[title="bob"]
Find elements of a particular type that have an attribute of a specific value
//a[contains(@title,'bob')]
a[title*="bob"]
Find elements of a particular type that have an attribute that contains a specific value
//a[starts-with(@title,'bob')]
a[title^="bob"]
Find elements of a particular type that have an attribute that starts with a specific value


Few Examples:
xpath=//div//span/a
css=div * span > a

xpath=//div[@id="some_id"]
css=div[id=some_id]

xpath=//div[contains(@title,"title")]
css=div[@title~=title]


<div id=some_id />
css=div#some_id

xpath=//div[@class="myclass"]
css=div.myclass


<div title="some multiword title" /> while the element like  <div title="my_title" />
can't be detected by CSS locator from above.

Note:
Things that XPath can do those CSS locators can’t:
Get an element’s parent, eg:  //div[@class='bob']/..

From the sample above we may see analogy for XPath and CSS namely:
a) Character "/"  meaning next level of DOM hierarchy corresponds to CSS operator ">".
b) Character "//" meaning any object hierarchy level under current object corresponds to CSS operator "*".


Sources:

Selenium : Opening different Browsers





Selenium: Opening Firefox, IE and Chrome Browsers


Firefox:

                FirefoxDriver oFFdriver=new FirefoxDriver();
                oFFdriver.get("http://www.google.com");         
                JOptionPane.showMessageDialog(null, "Done");

OR

-------------------------Code to Set Profile-------------------
ProfilesIni listprofile=new ProfilesIni();
FirefoxProfile profile=listprofile.getProfile("default");
WebDriver driver=new FirefoxDriver(profile);
driver.get("http://www.google.com");
----------------------------------------------------------------------
 



Internet Explorer

Note:
1.       You will need IE Driver for Selenium.
2.       Follow the link below and download the driver. http://code.google.com/p/selenium/downloads/list
3.       Extract it into the folder and use the path inside the code

System.setProperty("webdriver.ie.driver","Your_Path//IEDriverServer.exe");
WebDriver webdriver = new InternetExplorerDriver();
webdriver.manage().window().maximize();
webdriver.get("http://www.usatoday.com");

Chrome :

Note:
1.       You will need Chrome Driver for Selenium.
3.       Extract it into the folder and use the path inside the code


 ----------------Simple Code-----------------------------------------
System.setProperty("webdriver.chrome.driver","Path of Chrome driver");
//while giving path use "\\" instead of "\"

WebDriver driver=new ChromeDriver();
driver.get("http://wikipedia.org");



---------------------------------------------------------------------------

OR


 ------------Code to set profile in Chrome -------------------------
final String userProfile= "Your default -UserDate path in Chrome"     
           //"C:\\Users\\deepakj\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\";
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir="+userProfile);
options.addArguments("--start-maximized");
System.setProperty("webdriver.chrome.driver","http://www.usatoday.com");
webdriver = new ChromeDriver(options);
webdriver.manage().window().maximize();
webdriver.get(url);
----------------------------------------------------------------------------

Friday, November 22, 2013

Selenium : (Javascript) click method vs (Selenium) click method

(Javascript) click method  vs (Selenium) click method



Javascript click methods are much faster ,compared to Selenium click methods.Selenium users can implemet Javascipt click instead of traditional clicks of Selenium


JavaScript click

WebElement element = driver.findElement(By.id("something"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
 
 
 

Tuesday, November 19, 2013

Java: Use Enum in Switch Statement

Use Enum in Switch Statement


--------------------------------enumeration.java-------------------put it in a new file
public enum enumeration {
            SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
            THURSDAY, FRIDAY, SATURDAY
        }

------------------------------------------------------------------------




----------------------Test.java---------------------------------
public class Test {
    public static void main(String[] args)
    {           
        String s;
        s="SUNDAY1";
        try{
                if(enumeration.valueOf(s) != null)       
                    System.out.print("ya");
        }catch(Exception E){System.err.print("Not found "+E);}       
       
        }
}

---------------------------------------------------------

Understanding Performance Testing


Understanding Performance Testing

Follow the link below :

Jmeter : Configuring Jmeter for the first time

Configuring Jmeter for the first time




Java:Crash Course

FUN:ftp mp3 list

Friday, November 8, 2013

Eclipse : A Java Exception has occured

A Java Exception has occurred



Make sure you have you have the latest version of JRE installed in your system.


If the problem still persists then ,the reason being the JRE’s of your system might not be added to the project under consideration. Please follow the steps below .
1)      Eclipse Tool Bar>Window>Preferences> Java>Installed Jre’s.
2)      You should be seeing the path of the JRE installed.Now copy the location of installed JREs
3)      Close the window.
4)      Select your Project>Rt ck >Properties>Java Build Path
5)    In Libraries tab >  clk Add library button
6)      In Add Library Window>Select JRE System Library option>next
7)      In JRE System Library window >clk on Installed JREs > (you should be able to see the path of the insatlled JREs)  Select the JRE  path   
8)      Finish

OR

Other Things done if the above did not solve the problem : -
  A ) Eclipse Tool bar > Project >Clean
OR
  • Code view >Rt click>Run as Config
  • Select Java Application >select  java file containing main function >check all the check boxes in project
OR

  • §  right click project folder > properties
  • §  java compiler
  • §  select 1.7 (or whichever is relevant )