Selenium Lesson 33: TestNG Testing Framework for Selenium Part-2|Selenium Tutorial|G C Reddy|

preview_player
Показать описание
TestNG Annotation, @Test, @BeforeMethod, @AfterMethod, @BeforeClass, @AfterClass, @BeforeTest, @AfterTest for Executing TestNG Test Cases and Execute multiple TestNG Programs using XML file.
Automated Testing using Selenium WebDriver, Java and TestNG Testing Framework, Download & Install TestNG Framework in Eclipse IDE for Selenium and Write First TestNG Program in Eclipse.
Supported Testing Framework for Java Program, Introduction to TestNG Testing Framework, Install TestiNG for Selenium in Eclipse IDE, Features of TestNG and Advantages of TestNG Framework in Selenium Testing.
Write TestNG Test Cases using TestNG Annotations and priority Attributes, Prioritize Test Cases, Grouping Test Cases, Executing Test Batches and Generating Test Results using TestNG Testing Framework.
Рекомендации по теме
Комментарии
Автор

Class Notes:
Selenium Class 33: TestNG Testing Framework for Selenium Part-2

Part-1
i) Introduction to TestNG Testing Framework
ii) Install TestNG and write First TestNG Program
iii) Create multiple Test Case

Part-2
iv) TestNG Annotations
v) Execute multiple Programs/Classes using XML

iv) TestNG Annotations

@Test - The annotated method is a Test Case

@BeforeMethod - The annotated method will be run before each Test Case in the current class is invoked

@After Method - The annotated method will be run after each Test case in the current class have been run

Example:
Test Cases:
i) launchBrowser()
ii) verifyGoogleTitle()
iii) verify YahhoTitle()
iv) verify BankofamericaTitle()
v) closeBrowser()

Test Execution Flow:
i) closeBrowser()
ii) launchBrowser()
iii) verifyBankofamericaTitle()
iv) verifyGoogleTitle()
v) verifyYahhoTitle()

Required Test Flow:
i) launchBrowser()
ii) verifyGoogleTitle()
iii) verifyYahhoTitle()
iv) verifyBankofamericaTitle()
v) closeBrowser()

Ex: 1) TestNG Program as per Alphabetical Order

public class Class1 {
static WebDriver driver;
@Test
public void launchBrowser(){
System.setProperty("webdriver.edge.driver", "F:/MicrosoftWebDriver.exe");
driver = new EdgeDriver();
}
@Test
public void verifyGoogleTitle(){
Assert.assertEquals("Google", driver.getTitle());
}
@Test
public void verifyYahooTitle(){
Assert.assertEquals("Yahoo", driver.getTitle());
}
@Test
public void verifybankofAmericaTitle(){
Assert.assertEquals("Bank of America - Banking, Credit Cards, Home Loans and Auto Loans", driver.getTitle());
}
@Test
public void closeBrowser(){
driver.close();
}
}

Ex: 2) TestNG Program as per priority

public class Class1 {
static WebDriver driver;
@Test (priority =1)
public void launchBrowser(){
System.setProperty("webdriver.edge.driver", "F:/MicrosoftWebDriver.exe");
driver = new EdgeDriver();
}
@Test (priority =2)
public void verifyGoogleTitle(){
Assert.assertEquals("Google", driver.getTitle());
}
@Test (priority =3)
public void verifyYahooTitle(){
Assert.assertEquals("Yahoo", driver.getTitle());
}
@Test (priority =4)
public void verifybankofAmericaTitle(){
Assert.assertEquals("Bank of America - Banking, Credit Cards, Home Loans and Auto Loans", driver.getTitle());
}
@Test (priority =5)
public void closeBrowser(){
driver.close();
}
}

Ex: 3) TestNG Program using Before and After Method Annotations and priority

Test Cases:
i) launchBrowser()
ii) verifyGoogleTitle()
iii) verifyYahooTitle()
iv) verifyBankofamericaTitle()
v) closeBrowser()

Test Execution Flow:

launchBrowser()
i) verifyGoogleTitle()
closeBrowser()

launchBrowser()
ii) verifyYahooTitle()
closeBrowser()

launchBrowser()
iii) verifyBankofamericaTitle()
closeBrowser()

public class Class1 {
static WebDriver driver;
@BeforeMethod
public void launchBrowser(){
System.setProperty("webdriver.edge.driver", "F:/MicrosoftWebDriver.exe");
driver = new EdgeDriver();
}
@AfterMethod
public void closeBrowser(){
driver.close();
}
@Test (priority =1)
public void verifyGoogleTitle(){
Assert.assertEquals("Google", driver.getTitle());
}
@Test (priority =2)
public void verifyYahooTitle(){
Assert.assertEquals("Yahoo", driver.getTitle());
}
@Test (priority =3)
public void verifybankofAmericaTitle(){
Assert.assertEquals("Bank of America - Banking, Credit Cards, Home Loans and Auto Loans", driver.getTitle());
}
}

@BeforeClass - The annotated method will be run the first Test Method in the current Class is invoked

@AfterClass - The annotated method will be run after all the Test methods in the current class
have been run

Ex: 4) TestNG Program using Before and After Class Annotations and priority

Test execution Flow:
launchBrowser()
i) verifyGoogleTitle()
ii) verifyYahooTitle()
iii) verifyBankofamericaTitle()
closeBrowser()

TestNG Program:
public class Class1 {
static WebDriver driver;
@BeforeClass
public void launchBrowser(){
System.setProperty("webdriver.edge.driver", "F:/MicrosoftWebDriver.exe");
driver = new EdgeDriver();
}
@AfterClass
public void closeBrowser(){
driver.close();
}
@Test (priority =1)
public void verifyGoogleTitle(){
Assert.assertEquals("Google", driver.getTitle());
}
@Test (priority =2)
public void verifyYahooTitle(){
Assert.assertEquals("Yahoo", driver.getTitle());
}
@Test (priority =3)
public void verifybankofAmericaTitle(){
Assert.assertEquals("Bank of America - Banking, Credit Cards, Home Loans and Auto Loans", driver.getTitle());
}
}

@BeforeTest - The annotated method will be run before any Test method belonging to the classes
inside the tag is run

@AfterTest - the annotated method will be run after all the test methods belonging to classes
inside the tag

v) Execute multiple Programs/Classes using XML

> HTML and XML are tag based languages and no control flow in these languages
> HTML - Web Page Design (Web UI Design)
> XML - Data Transporting

XML file for executing multiple programs/classes

<suite name ="Suite Name">
<test name = "Test Name">
<classes>
<class name="package.class1Name"/>
<class name="package.class2Name"/>
.
</classes>
</test>
</suite>

Tags in this XML File

suite tag
test tag
classes tag
class

Writing XML Tags:

<tagName> - Open tag
</tagName> - Close tag
or
<tagName/> - Open and Close

Navigation to create an XML file in Eclipse

In Eclipse IDE,
Select Java package and right click >New>Other...
> Enter "TestNG"
> Enter source and package names
> Enter XML file name
> Finish

XML File:
<suite name="Ecommerce" parallel="false">
<test name="SanityTests">
<classes>
<class name="abcd.Class1"/>
<class name="abcd.Class2"/>

</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

Class Files: (Incomplete)

gcreddy