This blog is for Automation Test Engineers

Friday 16 March 2018

Writing data into excel sheet


FileInputStream fis=new FileInputStream("E:\\data.xlsx");              Workbook wb=WorkbookFactory.create(fis);
Sheet s=wb.getSheet("test");//sheet name
s.createRow(1).createCell(1).setCellValue("Java");
FileOutputStream fos = new  FileOutputStream("E:\\data.xlsx");
 wb.write(fos);
 fos.close();
System.out.println("Success");



                 }


2

How to click on Hyperlink

WebDriver driver= new FirefoxDriver();
driver.get("https://www.google.com"):
driver.findElement(By.linkText("ಕನ್ನಡ")).click();

0

Different types of navigation command


1)driver.navigate().back(): This command takes back the user to previous page of the application.
2)driver.navigate().forward(): This command navigates the user to next page of application
3)driver.navigate().refresh(); This command refreshes the current webpage
4)driver.navigate().to():This command allows the user to launch a new web browser window and navigates to specified URL
Eg:
driver.navigate().to("https://www.google.co.in/");


0

How to enter semicolon

String keys=keys.chord(Keys.SHIFT, Keys.SEMICOLON);

0

How to press Shift+Tab


String keys=keys.chord(Keys.SHIFT, Keys.TAB);

0

How do u read text from hidden element

First we should move cursor over element where element is hidden and we can use getText() method to get the text from hidden element

0

Windows scroll up/down


WebDriver driver= new FirefoxDriver();
driver.get("https://www.flipkart.com/"):
JavascriptExceutor js=new JavascriptExceutor ();
//scroll up
js.executeScript("window.scrollBy(0,450)","");
//scroll down
js.executeScript("window.scrollBy(450,0)","");

0

How to get size of object

WebDriver driver= new FirefoxDriver();
driver.get("https://www.google.com"):
WebElement button=driver.findElement(By.name("btnK"));
System.out.print(button.getSize());


0

How to get tag name

WebDriver driver= new FirefoxDriver();
driver.get("https://www.google.com"):
WebElement button=driver.findElement(By.name("btnK"));
System.out.print(button.getTagName());



0

How to get text of object

WebDriver driver= new FirefoxDriver();
driver.get("https://www.google.com"):
WebElement button=driver.findElement(By.name("btnK"));
System.out.print(button.getText());


0

How to get location of object



WebDriver driver= new FirefoxDriver();
driver.get("https://www.google.com"):
WebElement button=driver.findElement(By.name("btnK"));
System.out.print(button.getLocation());


0

How to get hashcode of object

WebDriver driver= new FirefoxDriver();
driver.get("https://www.google.com"):
WebElement button=driver.findElement(By.name("btnK"));
System.out.print(button.hashcode());


0

How to handle popup in selenium

WebDriver driver =new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.vrlbus.in/vrl2013/register_customer.aspx");
WebElement firstbox=driver.findElement(By.name("FIRSTNAME"));
firstbox.sendKeys("chaitra");
driver.findElement(By.xpath("//input[@type='submit']")).click();
String alertmessage=driver.switchTo().alert().getText();
System.out.print(alertmessage);
0

How to get title of page

System.setProperty("webdriver.ie.driver","E:\\IEDriverServer.exe";
Webdriver driver=new InternetExplorerDriver();
driver.get("https://www.flipkart.com/");
String title=driver.getTitle();
System.out.println(title);


0

How do you open an application in IE broser

System.setProperty("webdriver.ie.driver","E:\\IEDriverServer.exe";
Webdriver driver=new InternetExplorerDriver();
driver.get("https://www.flipkart.com/");

0

How do you open an application in chrome broser

System.setProperty("webdriver.chrome.driver","E:\\chrome.exe");
Webdriver driver=new ChromeDriver();
driver.get("https://www.flipkart.com/");

0

How to handle Autosuggestion in selenium

Webdriver driver =new FirefoxDriver();
driver.get("https://www.google.co.in");
WebElement searchbox=driver.finElement(By.name("q")).senkeys("selenium"));
List<WebElement>autosugg=driver.findElement(By.xpath("//div[contains(text(),'selenium')]
for(int i=1;i<autosugg.size();i++){

System.out.println(autosugg.get(i).getText());
}
0

How to minimize the screen size

Webdriver driver =new FirefoxDriver();
driver.get("https://www.google.co.in");
driver.manage().window().setPosition(new Point(0,-500));
driver.close();


0

How to maximize the screen in selenium

Webdriver driver =new FirefoxDriver();
driver.get("https://www.google.co.in");
driver.manage().window().maximize();
driver.close();

0

Thursday 15 March 2018

How to clear textbox in selenium

Webdriver driver=new FirefoxDriver();
driver.get("https://www.google.co.in/");
WebElement searchbox=driver.finElement(By.name("q")).senkeys("selenium"));
searchbox.sendkeys(keys.chord(Keys.Control,"a"));
searchbox.sendkeys(keys.BACK_SPACE);
 OR
searchbox.clear();
driver.close();


2

Fibonacci Sequence

int first=0;
int second=1;
int next;
Scanner in=new Scanner(System.in);
System.out.println("Enter the number");
int n=in.nextInt();
for(int i=o;i<n;i++){
next=first+second;
System.out.println(next);
first=second;
second=next;


0

Assertions in TesNG

Assertions plays very important role. It helps to verify the condition of your test. It the condition is not satisfied it fails the test and it will stop the execution.


Different TestNG Asserts Statements:


1)assertEquals(boolean actual,boolean expected) : Takes two boolean arguments and checks whether both are equal else it fails the test.

2)assertEquals(boolean actual,boolean expected, java.lang.String message): Takes two boolean arguments and checks whether both are equal else it fails the test with given string message.

3)assertEquals(java.lang.String actual,java.lang.String expected, java.lang.String message):  Takes two String arguments and checks whether both are equal else it fails the test with given string message.

4)assertTrue(boolean condition, java.lang.String message): verifies that condtion is true else fails the test with the given message

5)asserFalse(boolean condition, java.lang.String message):verifies that condtion is false else fails the test with the given message

6)fail(java.lang.String message): Directly it fails the test with the given message



0

Other Annotations in TestNG

alwaysRun
If set to true, this test method will always be run even if it depends on a method that failed.
dataProvider
The name of the data provider for this test method.
dataProviderClass
The class where to look for the data provider. If not specified, the data provider will be looked on the class of the current test method or one of its base classes. If this attribute is specified, the data provider method needs to be static on the specified class.
dependsOnGroups
The list of groups this method depends on.
dependsOnMethods
The list of methods this method depends on.
description
The description for this method.
enabled
Whether methods on this class/method are enabled.
expectedExceptions
The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.
groups
The list of groups this class/method belongs to.
invocationCount
The number of times this method should be invoked.
invocationTimeOut
The maximum number of milliseconds this test should take for the cumulated time of all the invocationcounts. This attribute will be ignored if invocationCount is not specified.
priority
The priority for this test method. Lower priorities will be scheduled first.
successPercentage
The percentage of success expected from this method
singleThreaded
If set to true, all the methods on this test class are guaranteed to run in the same thread, even if the tests are currently being run with parallel="methods". This attribute can only be used at the class level and it will be ignored if used at the method level. Note: this attribute used to be called sequential (now deprecated).
timeOut
The maximum number of milliseconds this test should take.
threadPoolSize
The size of the thread pool for this method. The method will be invoked from multiple threads as specified by invocationCount.
Note: this attribute is ignored if invocationCount is not specified



0

TestNG Suite


Inorder to execute multiple TestNG classes automatically one after the another you can use TestNG Suite.
TestNG suite is an xml file which contains list of TestNG classes which are to be executed. In order to create TestNG suite, right click on the package or on the java project go to TestNG---->Select convert to TestNG and click on finish which creates TestNG.xml inside the java project as shown below:


?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Testone">
<classes>
<class name="com.Firstpackage.Test "/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

0

How to execute first TestNG class


1)   Select the Java project in the Eclipse and go to Run | Run Configuration.Run Configurations window will be opened.




1)   Select TestNG in the given options and select a New_configuration.



3) Please notice that TestNG plugin provides multiple options for running your test cases as follows:
·         Class: Using this option you can provide the class name along with the package to run only the said specific test class.
·         Method: Using this you can run only a specific method in a test class.
·         Groups: In case you would like to run specific test methods belonging to a particular TestNG group, you can enter those here for executing them.
·         Package: If you would like to execute all the tests inside a package, you can specify these in this box.
·         Suite: In case you have suite files in the form of testing.xml files, you can select those here for execution.
Let’s enter the configuration name as TestNGRunConfig and select the newly created class under the Class section and click on Apply.
4) Now if you would like to run the newly created configuration, just click on Run after clicking on Apply. This will compile and run the TestNG test class that we have written. The result of the test execution is displayed in the Console and Results windows of Eclipse as shown in the following screenshot.

TestNg Results window




  
  1)TestNG also produce HTML reports. To access those reports go to the  Project directory and open test-output folder

   2)Open emailable-report.html, as this is a html report open it with browser.












0

Examples for all annotaions



import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
public class Test {

@BeforeMethod
public void beforeMethod() {
System.out.println("Before Method");
}

@AfterMethod
public void afterMethod() {
System.out.println("After Method");
}

@BeforeClass
public void beforeClass() {
System.out.println("Before Class");
}

@AfterClass
public void afterClass() {
System.out.println("After Class");
}

@BeforeTest
public void beforeTest() {
System.out.println("Before Test");
}
@AfterTest
public void afterTest() {
System.out.println("After Test");
}

@BeforeSuite
public void beforeSuite() {
System.out.println("Before Suite");
}
@AfterSuite
public void afterSuite() {
   System.out.println("After Suite");
}
}

0