Tuesday, 7 January 2014

How to pass parameters from testng.xml into test case


How to pass parameters from testng.xml into
 test case.

package programs;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class Parallelexecution {

private WebDriver driver = null;

@BeforeTest
@Parameters({ "BROWSER" })
public void setup(String BROWSER) {
System.out.println("Browser: " + BROWSER);

if (BROWSER.equals("FF")) {
System.out.println("Firefox Browser is selected");
driver = new FirefoxDriver();
} else if (BROWSER.equals("IE")) {
System.out.println("Internet Explorer Browser is selected");
driver = new InternetExplorerDriver();
} else if (BROWSER.equals("HU")) {
System.out.println("Html Unit Browser is selected");
driver = new HtmlUnitDriver();
} else if (BROWSER.equals("CH")) {
System.out.println("Google chrome Browser is selected");
driver = new ChromeDriver();
}
}

@Test
public void testParallel() throws Exception {
driver.get("http://ruchi-myseleniumblog.blogspot.in/2013/12/100-selenium-interview-questions.html");

}
}


above sample program BROWSER is a variable which value would be passed from TestNG.xml and TestNG.xml and it will run the test multiple time each time BROWSER value would be set with different browser name and test will check the BROWSER value and decide which browser test will run.
<?xml version="1.0" encoding="UTF-8"?><suite name="WebDriver-TestngParrlelExecution" parallel="tests">  <test name="Working Witn FF" preserve-order="true">  <parameter name="BROWSER" value="FF" />  <classes>   <class name="programs.Parallelexecution" />  </classes> </test>  <test name="Working with IE" preserve-order="ture">  <parameter name="BROWSER" value="IE"></parameter>  <classes>   <class name="programs.Parallelexecution"></class>  </classes> </test>  <test name="Working with HTML unit" preserve-order="true">  <parameter name="BROWSER" value="HU"></parameter>  <classes>   <class name="programs.Parallelexecution"></class>  </classes> </test>   <test name="Working with Chrome Browser" preserve-order="true">  <parameter name="BROWSER" value="CH"></parameter>  <classes>   <class name="programs.Parallelexecution"></class>  </classes> </test></suite>

How to get the name of browser using Web Driver

Is there any method or any way to get the name of browser using Web Driver?
public class JsExecute
{
WebDriver driver;
JavascriptExecutor js;
@Before
public void setUp() throws Exception
{
driver=new FirefoxDriver();
driver.get("http://www.google.com");
}
@Test
public void test()
{
JavascriptExecutor js = (JavascriptExecutor) driver;
System.out.println(js.executeScript("return navigator.appCodeName"));
}}
OR
String s = (String) ((JavascriptExecutor) driver).executeScript("return navigator.userAgent;");
System.out.println("Browser name : " + s);




How to handle autocomplete box in web driver

How to handle autocomplete box in web driver?

driver.findElement(By.id("your searchBox")).sendKeys("your partial keyword");
Thread.sleep(3000);
List <WebElement> listItems = driver.findElements(By.xpath("your list item locator"));
listItems.get(0).click();
driver.findElement(By.id("your searchButton")).click();



How to find broken images in a page using Selenium

package programs;

import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class findbrokenimages {
static int invalidimg;
static WebDriver driver ;
public static void main(String[] args) {
try {
driver = new FirefoxDriver();
driver.get("http://ruchi-myseleniumblog.blogspot.in");
invalidimg = 0;
List allImages  = driver.findElements(By.tagName("img"));
System.out.println("Total  images are " + allImages.size());
for (int i = 0; i < allImages.size(); i++) {
WebElement img = (WebElement) allImages.get(i);
if (img != null) {
verifyimgActive(img);
}
}

System.out.println("Total invalid images are " + invalidimg);
driver.quit();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}



public static void verifyimgActive(WebElement img) {
try {
HttpResponse response = new DefaultHttpClient().execute(new HttpGet(img.getAttribute("src")));
   if (response.getStatusLine().getStatusCode() != 200)
invalidimg++;
}
catch (Exception e) {
e.printStackTrace();
}
}
}


Back to Selenium Interview Questions and Answers

How to refresh a page without using context click

How to refresh a page without using context click?

1.Using sendKeys.Keys method
driver.get("https://accounts.google.com/SignUp");
driver.findElement(By.id("firstname-placeholder")).sendKeys(Keys.F5);

2.Using navigate.refresh() method
driver.get("http://ruchi-myseleniumblog.blogspot.in/2013/12/100-selenium-interview-questions.html");
driver.navigate().refresh();

3.Using navigate.to() method
driver.get("http://ruchi-myseleniumblog.blogspot.in/2014/01/selenium-hybrid-framework-using.html");
driver.navigate().to(driver.getCurrentUrl());

4.Using get() method
driver.get("http://ruchi-myseleniumblog.blogspot.in/2013/12/basic-core-java-interview-questions.html");
driver.get(driver.getCurrentUrl());

5.Using sendKeys() method
driver.get("https://accounts.google.com/SignUp");

driver.findElement(By.id("firstname-placeholder")).sendKeys("\uE035");



hierarchy of TestNG annotations

“What’s the hierarchy of TestNG annotations? Explain me about annotation hierarchy & execution order?


Please find hierarchy below:

  1. org.testng.annotations.Parameters (implements java.lang.annotation.Annotation)
  2. org.testng.annotations.Listeners (implements java.lang.annotation.Annotation)
  3. org.testng.annotations.Test (implements java.lang.annotation.Annotation)
  4. org.testng.annotations.AfterMethod (implements java.lang.annotation.Annotation)
  5. org.testng.annotations.BeforeTest (implements java.lang.annotation.Annotation)
  6. org.testng.annotations.BeforeMethod (implements java.lang.annotation.Annotation)
  7. org.testng.annotations.Optional (implements java.lang.annotation.Annotation)
  8. org.testng.annotations.AfterTest (implements java.lang.annotation.Annotation)
  9. org.testng.annotations.Guice (implements java.lang.annotation.Annotation)
  10. org.testng.annotations.BeforeGroups (implements java.lang.annotation.Annotation)
  11. org.testng.annotations.ExpectedExceptions (implements java.lang.annotation.Annotation)
  12. org.testng.annotations.TestInstance (implements java.lang.annotation.Annotation)
  13. org.testng.annotations.NoInjection (implements java.lang.annotation.Annotation)
  14. org.testng.annotations.AfterSuite (implements java.lang.annotation.Annotation)
  15. org.testng.annotations.AfterClass (implements java.lang.annotation.Annotation)
  16. org.testng.annotations.AfterGroups (implements java.lang.annotation.Annotation)
  17. org.testng.annotations.DataProvider (implements java.lang.annotation.Annotation)
  18. org.testng.annotations.BeforeSuite (implements java.lang.annotation.Annotation)
  19. org.testng.annotations.BeforeClass (implements java.lang.annotation.Annotation)
  20. org.testng.annotations.Factory (implements java.lang.annotation.Annotation)
  21. org.testng.annotations.Configuration (implements java.lang.annotation.Annotation)
  22. org.testng.annotations.ObjectFactory (implements java.lang.annotation.Annotation)

How to run tests in multiple browser parallel

--TBD