Wednesday, March 27, 2013

Replace XPath with Css selector






If you know css selectors, those are much more easier (and will work fine with IE without any problem ) than xpath even though they can't replace entire xpath.


Consider below HTML tags


 <div id="firstDiv">This div tag has static id </div>

 <p id="paragraps_dynamic_1234">This p tag has dynamic id</p>

 <a onclick="doThat()" title="doThatOperation">This a tag has multiple attributes</a>

 <h1 name="heading1" value="headName">This is heading</h1>


<div>
<input type="text" id="username" name="username"
<input type="password" id="password" name="pwd"
</div>

<h2 id="propertyUserData">This tag is for starts-with example</h2>


Tag XPATH CSS SELECTOR
div "//div[@id='firstDiv']" "div[id='firstDiv']"
p "//p[contains(@id,'paragraps_dynamic_')]" "p[id*='paragraps_dynamic_']"
a "//a[contains(@onclick,'doThat') and @tile='doThatOperation']" "a[onclick*='doThat'][tile='doThatOperation']"
h1 "//h1[@name='heading1' and @value='headName']" "h1[name='heading1'][value='headName']"
input "//div/input[@id='username']" "div > input[id='username']"
input "//h2[starts-with(@id,'propertyUser')]" "h2[id^='propertyUser']"

By seeing above locators we can conclude some points regarding css selectors

  • Using css selector we can access particular node or immediate child of any node
  • Can combine as many conditions as we want for single node.
  • Can achieve starts-with, contains functionality using ^,* symbols respectively. 
  • We can't traverse back using css selector.
  • We can't go to  the siblings also (preceding as well as following)



Tuesday, March 26, 2013

Running JUnit test cases from cmd prompt





Running JUnit test cases from cmd prompt
For Running Junit 4.x test cases from command prompt you should use below command

java -cp C:\lib\junit.jar org.junit.runner.JUnitCore [test class name]

For running Junit 3.x test cases from command prompt you need use below command

java -cp /usr/java/lib/junit.jar junit.textui.TestRunner [test class name]

Example Program

TestCaseA.java

package test; import org.junit.Test; import org.junit.After; import org.junit.Before; public class TestCaseA { @Before public void beforeMethod() { System.out.println("Before method.."); } @Test public void JUnit4Test() { System.out.println("In test method"); } @After public void afterMethod() { System.out.println("after method"); } }

Execution from command prompt

java -cp junit-4.10.jar;test\TestCaseA.class; org.junit.runner.JUnitCore test.TestCaseA




Regards,
SantoshSarma J V