Coding for Tester : Getting Started

Every wondered how people switch from Manual to Automation testing. I have to work as the Manual tester for 1 year after I switched to automation testing, How I used daily hacks to change my career towards automation. Little glance in this video

Hope you all like it.

 

Selenium Test Cases in Java with TestNG

TestNG is next generation testing framework like JUnit & NUnit but with extra features(Read keywords, Generate Test Reports, Generate logs) and easy to use. TestNG framework can cover different type of the tests like Integration, Unit, end-to-end, etc.

  Installing TestNG in Eclispe:

  • Open Eclipse , Select Help / Software updates / Find and Install.
  • Search for new features to install.
  • New remote site.
  • For Eclipse 3.4 and above, enter http://beust.com/eclipse.
  • For Eclipse 3.3 and below, enter http://beust.com/eclipse1.
  • Make sure the check box next to URL is checked and click Next.
  • Eclipse will then guide you through the process.

We are going to Test a Demo website “http://demoaut.com/mercuryregister.php” and will write a Test case
to Do a Registration in it.

Project Structure:

Step1:
Create a project in the eclispe. Add the Selenium StandAlone jar and TestNG jar into a new libs  folder.
Make sure you add them to Build Path by Right Click – Build Path –> Add to Build Path.

Step2:
Create a Base Class with Test Fixture methods e.g. Setup and Tear Down. Note: I have added function name
as JUnit you can name them as you want), Add annotations as Before Method and After Method.

[java]
package com.scrolltest.testng;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class BaseClass {

WebDriver driver;

@BeforeMethod
public void setUp() {
driver = new FirefoxDriver();

}

@AfterMethod
public void tearDown() {
// driver.close();
driver.quit();
}

}
[/java]

Step3 :
Create a Separate Class with Name Registration .java to write a Test case for Registration of the
Demo: Website http://demoaut.com/mercuryregister.php

[java]
package com.scrolltest.testng;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.Reporter;

public class RegisterationPageObect {

private WebDriver driver;

public RegisterationPageObect(WebDriver driver) {
this.driver = driver;
}

public void RegisterUser() {
driver.findElement(By.name("firstName")).sendKeys("Pramod");
driver.findElement(By.name("lastName")).sendKeys("Dutta");
driver.findElement(By.name("phone")).sendKeys("981457452");
WebElement t = driver.findElement(By.id("userName"));

driver.findElement(By.id("userName")).sendKeys("a@c.com");
Reporter.log(""+t.getAttribute("maxlength"));
// #Drop Down
new Select(driver.findElement(By.name("country")))
.selectByVisibleText("ANGUILLA");
// register
driver.findElement(By.name("register")).click();
}

}[/java]

Step4: Now Lets create a Final class and TestNG xml file.

MainClass.java

[java]
package com.scrolltest.testng;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class MainClass extends BaseClass {

@Test
public void testRegisterationInDemoAUT() throws InterruptedException {

RegistrationPageObect pageOject = new RegistrationPageObect(driver);
driver.get("http://demoaut.com/mercuryregister.php");
pageOject.RegisterUser();

assert driver
.findElement(By.tagName("body"))
.getText()
.contains(
"Thank you for registering. You may now sign-in using the user name and password you’ve just entered.");

assert driver.findElement(By.tagName("body")).getText()
.contains("Dear Pramod Dutta,");

}

}
[/java]

Step5:
Now Run the Test by Creating new TestNG configuration by Clicking on the Project->Run Configuration, Select
the TestSuite and browse the XML file(testng.xml). and Now Run the Project.

Step6:
After run successfully, If you refresh you project you will see the test-output folder , Open “index.html” and see that You Test Suite Results. Click on the Report output link you can see max length also.(Used as Logging)

Source code Github