@Test(enable=false)
Tuesday, 29 December 2015
Difference between 'final' and 'finally'?
final:
final is a keyword. The variable decleared as final should be
initialized only once and cannot be changed. Java classes
declared as final cannot be extended. Methods declared as final
cannot be overridden.
finally:
finally is a block. The finally block always executes when the
try block exits. This ensures that the finally block is executed
even if an unexpected exception occurs.
Difference between 'implicit wait' and 'explicit wait'? in selenium
In Implicit Wait , we define a code to wait for a certain amount of time when trying to find an element or elements. If the Web Driver cannot find it immediately because of its availability, the WebDriver will wait. The default setting is zero. Once we set a time, the Web Driver waits for the period of the WebDriver object instance.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
In Explicit Wait, we write a code to define a wait statement for certain condition to be satisfied until the wait reaches its timeout period. If WebDriver can find the element before the defined timeout value, the code execution will continue to next line of code. Therefore, it is important to setup a reasonable timeout seconds according to the system response.
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(by));
Exaplain about '@DataProvider'?
Exaplain about '@DataProvider'?
Ans: A DataProvider is data feeder method defined in your class that supplies a test method with data .
The annotated method must return an Object[][] where each Object[] can be assigned the parameter list of the test method.
Example Script:
@DataProvider(name = "DP1")
public Object[][] createData() {
Object[][] retObjArr={{"001","Jack","London"},
{"002","John","New York"},
{"003","Mary","Miami"},
{"004","George","california"}};
return(retObjArr);
}
@Test (dataProvider = "DP1")
public void testEmployeeData(String empid, String empName, String city){
selenium.type("id", empid);
selenium.type("name", empName);
selenium.click("submit_button");
selenium.waitForPageToLoad("30000");
assertTrue(selenium.isTextPresent(city));
}
++++++++Sample code using @DataProvider+++++
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Data_Provider {
private WebDriver driver;
private String baseUrl;
// String baseUrl, nodeUrl;
@DataProvider(name = "myTest")
public Object[][] createData1() {
return new Object[][] {
{ "India"},
{ "Brazil"},
{ "Canada"},
{ "Sri Lanka"},
{ "England"},
{ "UK"},
{ "United States"},
};
}
@BeforeMethod
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.wikipedia.org/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test(dataProvider = "myTest")
public void testSearchCountry(String country) throws Exception {
driver.get(baseUrl + "/wiki/Main_Page");
driver.findElement(By.id("searchInput")).clear();
driver.findElement(By.id("searchInput")).sendKeys(country);
driver.findElement(By.id("searchButton")).click();
String str = driver.findElement(By.cssSelector("span")).getText();
Assert.assertTrue(country.equals(str.trim()));
System.out.println(str);
}
@AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
}
++++++++Sample code using @DataProvider+++++
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Data_Provider {
private WebDriver driver;
private String baseUrl;
// String baseUrl, nodeUrl;
@DataProvider(name = "myTest")
public Object[][] createData1() {
return new Object[][] {
{ "India"},
{ "Brazil"},
{ "Canada"},
{ "Sri Lanka"},
{ "England"},
{ "UK"},
{ "United States"},
};
}
@BeforeMethod
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.wikipedia.org/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test(dataProvider = "myTest")
public void testSearchCountry(String country) throws Exception {
driver.get(baseUrl + "/wiki/Main_Page");
driver.findElement(By.id("searchInput")).clear();
driver.findElement(By.id("searchInput")).sendKeys(country);
driver.findElement(By.id("searchButton")).click();
String str = driver.findElement(By.cssSelector("span")).getText();
Assert.assertTrue(country.equals(str.trim()));
System.out.println(str);
}
@AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
}
TestNG annotations and TestNG annotations execution work flow
1) 'Test NG' Annotations?
Ans:
1. @BeforeSuite
2. @AfterSuite
3. @BeforeTest
4. @AfterTest
5. @BeforeGroups
6. @AfterGroups
7. @BeforeClass
8. @AfterClass
9. @BeforeMethod
10. @AfterMethod
11. @DataProvider
12. @Factory
13. @Listeners
14. @Parameters
15. @Test
Execution WorkFlow:
package interview;
import org.junit.BeforeClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestNG_Annotation_Execution_Flow
{
@Test
public void testCase1()
{
System.out.println("in test case1 & now am in 'test method' ");
}
@Test
public void testCase2()
{
System.out.println("in test case2 & now am in 'test method2' ");
}
@BeforeMethod
public void beforeMethod()
{
System.out.println("Now am in before Method");
}
@AfterMethod
public void afterMethod()
{
System.out.println("Now am in 'afetr Method' ");
}
@BeforeClass
public void beforeClass()
{
System.out.println("Now am in before class");
}
@BeforeClass
public void afterClass()
{
System.out.println("Now am in after class");
}
@BeforeTest
public void beforeTest() {
System.out.println("Now am in beforeTest");
}
@AfterTest
public void afterTest() {
System.out.println("Now am in after Test");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("Now am in beforeSuite");
}
@AfterSuite
public void afterSuite() {
System.out.println("Now am in after Suite");
}
}
Output:
Now am in beforeSuite
Now am in beforeTest
Now am in before Method
in test case1 & now am in 'test method'
Now am in 'afetr Method'
Now am in before Method
in test case2 & now am in 'test method2'
Now am in 'afetr Method'
Now am in after Test
PASSED: testCase1
PASSED: testCase2
Execution WorkFlow:
package interview;
import org.junit.BeforeClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestNG_Annotation_Execution_Flow
{
@Test
public void testCase1()
{
System.out.println("in test case1 & now am in 'test method' ");
}
@Test
public void testCase2()
{
System.out.println("in test case2 & now am in 'test method2' ");
}
@BeforeMethod
public void beforeMethod()
{
System.out.println("Now am in before Method");
}
@AfterMethod
public void afterMethod()
{
System.out.println("Now am in 'afetr Method' ");
}
@BeforeClass
public void beforeClass()
{
System.out.println("Now am in before class");
}
@BeforeClass
public void afterClass()
{
System.out.println("Now am in after class");
}
@BeforeTest
public void beforeTest() {
System.out.println("Now am in beforeTest");
}
@AfterTest
public void afterTest() {
System.out.println("Now am in after Test");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("Now am in beforeSuite");
}
@AfterSuite
public void afterSuite() {
System.out.println("Now am in after Suite");
}
}
Output:
Now am in beforeSuite
Now am in beforeTest
Now am in before Method
in test case1 & now am in 'test method'
Now am in 'afetr Method'
Now am in before Method
in test case2 & now am in 'test method2'
Now am in 'afetr Method'
Now am in after Test
PASSED: testCase1
PASSED: testCase2
Annotation
|
Description
| |
@BeforeSuite
|
The annotated method will be run only once before all tests in this suite have run.
| |
@AfterSuite
|
The annotated method will be run only once after all tests in this suite have run.
| |
@BeforeClass
|
The annotated method will be run only once before the first test method in the current class is invoked.
| |
@AfterClass
|
The annotated method will be run only once after all the test methods in the current class have been run.
| |
@BeforeTest
|
The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
| |
@AfterTest
|
The annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.
| |
@BeforeGroups
|
The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
| |
@AfterGroups
|
The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
| |
@BeforeMethod
|
The annotated method will be run before each test method.
| |
@AfterMethod
|
The annotated method will be run after each test method.
| |
@DataProvider
|
Marks a method as supplying data for a test method. The annotated method must return an Object[ ][ ] where each Object[ ] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.
| |
@Factory
|
Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[ ].
| |
@Listeners
|
Defines listeners on a test class.
| |
@Parameters
|
Describes how to pass parameters to a @Test method.
| |
@Test
|
Marks a class or a method as part of the test.
E | |
Subscribe to:
Posts (Atom)