Session 5: Intro to TestNG
- Introduction to TestNG
- Building a TestNG Framework
- TestNG Annotations (@BeforeMethod, @AfterMethod etc)
- Adding Priority to Sequential run (Priority = 1)
- TestNG reports including emailable reports
- Intro to xml file (if possible)
- Demo Test case: TBD
- Class activity: (group)
- Build a Maven TestNG Project
- Add test case for Login (More if possible)
- Upload the project to your personal Github account
- Homework: TBD
Class Notes
testng.pptx |
Prereq: TestNG plugin for Eclipse
Instruction:
1. Open Eclipse> click on Windows > Click on Preferences
2. Check to see if TestNG is installed, if not
a. Click on Help > Eclipse Marketplace
b. Search for TestNG
c. Click on "Browse for more solution."
d. Click on TestNG for Eclipse globe icon
e. Click on download icon under the "Install" word
f. Copy the url: http://beust.com/eclipse
AND/OR
a. Click on Help> Install New Software...
b. Click on Add
c. Type Name: TestNG
Location: http://beust.com/eclipse
d. Click Add
e. Select the TestNG check box
f. Click Next, Next, Finish, and Install Anyway.
g. Restart Eclipse and redo steps 1 and 2 to validate TestNG presence.
1. Open Eclipse> click on Windows > Click on Preferences
2. Check to see if TestNG is installed, if not
a. Click on Help > Eclipse Marketplace
b. Search for TestNG
c. Click on "Browse for more solution."
d. Click on TestNG for Eclipse globe icon
e. Click on download icon under the "Install" word
f. Copy the url: http://beust.com/eclipse
AND/OR
a. Click on Help> Install New Software...
b. Click on Add
c. Type Name: TestNG
Location: http://beust.com/eclipse
d. Click Add
e. Select the TestNG check box
f. Click Next, Next, Finish, and Install Anyway.
g. Restart Eclipse and redo steps 1 and 2 to validate TestNG presence.
Prereq: Maven Integration for Eclipse
Instruction:
a. Click on Help> Eclipse Marketplace
b. Search "Maven Integration for Eclipse"
c. Click on Install
d. Follow the system prompt
e. Install Anyway
f. Restart Eclipse
a. Click on Help> Eclipse Marketplace
b. Search "Maven Integration for Eclipse"
c. Click on Install
d. Follow the system prompt
e. Install Anyway
f. Restart Eclipse
Build Maven TestNG Project
Prerequisites:
1. JDK 8 installed and environment set (Systems variable: JAVA_HOME/JDK, Path/bin),
2. Maven installed and environment set (Systems variable: M2_HOME/maven, path/bin),
3. Eclipse configured with JDK 8 (Window>Preferences>Java>Installed JRE = JKD 8),
4. Eclipse configured with Maven (Help> Eclipse Marketplace > Install Maven Integration for Eclipse), &
5. Eclipse configured with TestNG plugin (Help> Eclipse Marketplace > Install TestNG for Eclipse).
2. Maven installed and environment set (Systems variable: M2_HOME/maven, path/bin),
3. Eclipse configured with JDK 8 (Window>Preferences>Java>Installed JRE = JKD 8),
4. Eclipse configured with Maven (Help> Eclipse Marketplace > Install Maven Integration for Eclipse), &
5. Eclipse configured with TestNG plugin (Help> Eclipse Marketplace > Install TestNG for Eclipse).
Steps:
Step 1: Create a Maven Project with group ID and Artifact ID (See session 1),
Step 2: Import Dependencies (Selenium-java and TestNG) in POM file and click Ctrl+S to save/import,
Step 3: Right click on project > Run As > Maven Build > Goals: clean install > Run,
Step 4: Create a folder called "driver" and save the chromedriver.exe file in that folder.
Step 5: Create a "test" package inside the test folder
Step 6: Create LoginTest class inside "test" package,
Step 7: Add test case: Valid users should be able to login (Or copy code from another project)
Step 8: Upload the project onto your personal Github Account
Step 2: Import Dependencies (Selenium-java and TestNG) in POM file and click Ctrl+S to save/import,
Step 3: Right click on project > Run As > Maven Build > Goals: clean install > Run,
Step 4: Create a folder called "driver" and save the chromedriver.exe file in that folder.
Step 5: Create a "test" package inside the test folder
Step 6: Create LoginTest class inside "test" package,
Step 7: Add test case: Valid users should be able to login (Or copy code from another project)
Step 8: Upload the project onto your personal Github Account
Class Code
Class: BasePage
public void waitForElement(WebDriver driver, int timeInSeconds, By locator) {
WebDriverWait wait = new WebDriverWait(driver, timeInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}
Class: LoginTest
WebDriver driver;
@BeforeMethod
public void startBrowser() {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://techfios.com/test/billing/?ng=admin/");
}
@Test(priority = 1)
public void validUserShouldBeAbleToLogin() throws InterruptedException {
driver.findElement(By.xpath("//input[@placeholder='Email Address']")).sendKeys("[email protected]");
driver.findElement(By.xpath("//input[@type='password']")).sendKeys("abc123");
driver.findElement(By.xpath("//button[contains(text(), 'Sign')]")).click();
waitForElement(driver, 10, By.xpath("//h2[contains(text(), 'Dashboard')]"));
}
@Test(priority = 2)
public void invalidUsersShouldNotBeAbleToLogin() throws InterruptedException {
driver.findElement(By.xpath("//input[@placeholder='Email Address']")).sendKeys("[email protected]");
driver.findElement(By.xpath("//input[@type='password']")).sendKeys("sfdgdfgdfgdg");
driver.findElement(By.xpath("//button[contains(text(), 'Sign')]")).click();
By ALERT_MSG_LOCATOR = By.xpath("//div[@class='alert alert-danger fade in']");
waitForElement(driver, 10, ALERT_MSG_LOCATOR);
}
@AfterMethod
public void close() {
driver.close();
driver.quit();
}
Class: SelectTest
WebDriver driver;
@BeforeMethod
public void startBrowser() {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://objectspy.space/");
}
@Test
public void selectTest() throws InterruptedException {
//Select South America
Select select = new Select(driver.findElement(By.id("continents")));
select.selectByIndex(4);
select.selectByValue("Sharif");
Thread.sleep(10000);
}
@AfterMethod
public void close() {
driver.close();
driver.quit();
}
public void waitForElement(WebDriver driver, int timeInSeconds, By locator) {
WebDriverWait wait = new WebDriverWait(driver, timeInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}
Class: LoginTest
WebDriver driver;
@BeforeMethod
public void startBrowser() {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://techfios.com/test/billing/?ng=admin/");
}
@Test(priority = 1)
public void validUserShouldBeAbleToLogin() throws InterruptedException {
driver.findElement(By.xpath("//input[@placeholder='Email Address']")).sendKeys("[email protected]");
driver.findElement(By.xpath("//input[@type='password']")).sendKeys("abc123");
driver.findElement(By.xpath("//button[contains(text(), 'Sign')]")).click();
waitForElement(driver, 10, By.xpath("//h2[contains(text(), 'Dashboard')]"));
}
@Test(priority = 2)
public void invalidUsersShouldNotBeAbleToLogin() throws InterruptedException {
driver.findElement(By.xpath("//input[@placeholder='Email Address']")).sendKeys("[email protected]");
driver.findElement(By.xpath("//input[@type='password']")).sendKeys("sfdgdfgdfgdg");
driver.findElement(By.xpath("//button[contains(text(), 'Sign')]")).click();
By ALERT_MSG_LOCATOR = By.xpath("//div[@class='alert alert-danger fade in']");
waitForElement(driver, 10, ALERT_MSG_LOCATOR);
}
@AfterMethod
public void close() {
driver.close();
driver.quit();
}
Class: SelectTest
WebDriver driver;
@BeforeMethod
public void startBrowser() {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://objectspy.space/");
}
@Test
public void selectTest() throws InterruptedException {
//Select South America
Select select = new Select(driver.findElement(By.id("continents")));
select.selectByIndex(4);
select.selectByValue("Sharif");
Thread.sleep(10000);
}
@AfterMethod
public void close() {
driver.close();
driver.quit();
}