1)TestNG
#1 If we have 2 methods with no priority defined, execution happens by Alphabetical Order
Ex Add_Test() will get executed before Div_Test() irespective of fact what order they have in script
#2 @Test(priority=1) is highest priority. it will get executed before @Test(priority=2)
* If two tests have @Test(priority=x) and third does not. Third is getting executed first
#3 @BeforeMethod executes before every method
#4 @AfterMethod executes after every method
#5 @BeforeTest executes Once before number of tests.
ex. We want to create db connection before starting execution
#6AfterTest We would like to close opened connection. Will execute after all tests have been executed
#7 Assert should be used for reporting Failure or Pass. Without them its not possible to know results
As soon as first assert fails test case is marked as failed. It wont got to next step.
**Executing with SoftAssert will shows only passes steps. test moves on to next step despite failure
SoftAssert sa=new SoftAssert();
sa.AssertEquals() pass
sa.AssertEquals() failed
sa.AssertEquals() pass
=>Report will show only passed data
#8 If we want to shows failed steps only then we need to use at end of above script
Sa.assertAll
#9@Test(dependsonMethods="methodname")
This is hard depedency. It method with methodname fails, the current test will not get executed
Above setup adds data like: 1 Failed, 1 Skipped
Soft Depedency:
@Test(dependsonMethods="methodname",alwaysrun=true)
#9 When we write click on project, we get convert to testng as one of the option.
By defaults its adds all the classes in default testng.xml where we have used @Test annotation.
We can create multiple suites by adding removing classes and create a mster xml.
<suite name="MasterSuite">
<suite-files>
<suite-file path="testng.xml" />
<suite-file path="testng2.xml"/>
</suite-files>
</suite>
#10
We can give group to the test so that we can see them in report according to that.
One test can have multiple groups
Ex 1 @Test(priority=1,groups={"grp1","grp2"})
Ex 2 @Test(priority=1,groups="grp1")
#NOw if we want to run only cases belonging to that group then we can modify xml file as below
<suite name="Suite">
<run>
<include name="login_related"</include>
</run>
<test thread-count="5" name="Test">
<classes>
<class name="com.totaltestng.cases.TestBase" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
# We can also exclude other cases except this group by replacing include with exclude
# groups can be defined at suite level or at test level
<suite name="Suite">
<test thread-count="5" name="Test">
<groups>
<run>
<exclude name="loginPage"></exclude>
</run>
</groups>
#11 Listeners
Suppose we want to take a screenshot on failure
1. Create a new class and implement ITestListner.
Add unimplemeted methods.
# in test failure method add yor code
# in respective testng file add
<listeners>
<listener class-name="com.totaltestng.listeners.TestngListener"/>
</listeners>
#12:
Forcefully skipping a test case
$ We can use throw SkipException to skip the test
@Test
public void testToSkip() {
int i = 4;
if (i == 4) {
throw new SkipException("Skipping forcefully with SkipException");
}
SKIPPED: testToSkip
org.testng.SkipException: Skipping forcefully with SkipException
#12
Testng by default provides Reporter object
#13 <parameter name="browser" value="chrome"/>
<parameter name="os" value="windows 7"/>\\
@Parameters({"browser","os"})
@Test
public void xmlPara(String browser, String os) {
System.out.println("BROWSER IS :"+browser+" OS:"+os);
}
#14 Parallel test
Testng helps in paraller execution
Grid helps in distributing tests on remote machine
parallet="tests" on suite line
#15
We can use paralle with data provider
@DataProvider(parallel=true)
#16 Invocationcount=5
executes 5 time
Invocationcount=5,threadPoolSize=5
lauches browsers 5 time
^[0-9|\s]|[0-9|\s]$
chrome default profile
C:\Users\config\AppData\Local\Google\Chrome\User Data\Default
System.setProperty("webdriver.chrome.driver", "H:\\chromedriver (2).exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:\\Users\\config\\AppData\\Local\\Google\\Chrome\\User Data\\Default");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
driver.get("https://www.zoho.com/");
#1 If we have 2 methods with no priority defined, execution happens by Alphabetical Order
Ex Add_Test() will get executed before Div_Test() irespective of fact what order they have in script
#2 @Test(priority=1) is highest priority. it will get executed before @Test(priority=2)
* If two tests have @Test(priority=x) and third does not. Third is getting executed first
#3 @BeforeMethod executes before every method
#4 @AfterMethod executes after every method
#5 @BeforeTest executes Once before number of tests.
ex. We want to create db connection before starting execution
#6AfterTest We would like to close opened connection. Will execute after all tests have been executed
#7 Assert should be used for reporting Failure or Pass. Without them its not possible to know results
As soon as first assert fails test case is marked as failed. It wont got to next step.
**Executing with SoftAssert will shows only passes steps. test moves on to next step despite failure
SoftAssert sa=new SoftAssert();
sa.AssertEquals() pass
sa.AssertEquals() failed
sa.AssertEquals() pass
=>Report will show only passed data
#8 If we want to shows failed steps only then we need to use at end of above script
Sa.assertAll
#9@Test(dependsonMethods="methodname")
This is hard depedency. It method with methodname fails, the current test will not get executed
Above setup adds data like: 1 Failed, 1 Skipped
Soft Depedency:
@Test(dependsonMethods="methodname",alwaysrun=true)
#9 When we write click on project, we get convert to testng as one of the option.
By defaults its adds all the classes in default testng.xml where we have used @Test annotation.
We can create multiple suites by adding removing classes and create a mster xml.
<suite name="MasterSuite">
<suite-files>
<suite-file path="testng.xml" />
<suite-file path="testng2.xml"/>
</suite-files>
</suite>
#10
We can give group to the test so that we can see them in report according to that.
One test can have multiple groups
Ex 1 @Test(priority=1,groups={"grp1","grp2"})
Ex 2 @Test(priority=1,groups="grp1")
#NOw if we want to run only cases belonging to that group then we can modify xml file as below
<suite name="Suite">
<run>
<include name="login_related"</include>
</run>
<test thread-count="5" name="Test">
<classes>
<class name="com.totaltestng.cases.TestBase" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
# We can also exclude other cases except this group by replacing include with exclude
# groups can be defined at suite level or at test level
<suite name="Suite">
<test thread-count="5" name="Test">
<groups>
<run>
<exclude name="loginPage"></exclude>
</run>
</groups>
#11 Listeners
Suppose we want to take a screenshot on failure
1. Create a new class and implement ITestListner.
Add unimplemeted methods.
# in test failure method add yor code
# in respective testng file add
<listeners>
<listener class-name="com.totaltestng.listeners.TestngListener"/>
</listeners>
#12:
Forcefully skipping a test case
$ We can use throw SkipException to skip the test
@Test
public void testToSkip() {
int i = 4;
if (i == 4) {
throw new SkipException("Skipping forcefully with SkipException");
}
SKIPPED: testToSkip
org.testng.SkipException: Skipping forcefully with SkipException
#12
Testng by default provides Reporter object
#13 <parameter name="browser" value="chrome"/>
<parameter name="os" value="windows 7"/>\\
@Parameters({"browser","os"})
@Test
public void xmlPara(String browser, String os) {
System.out.println("BROWSER IS :"+browser+" OS:"+os);
}
#14 Parallel test
Testng helps in paraller execution
Grid helps in distributing tests on remote machine
parallet="tests" on suite line
#15
We can use paralle with data provider
@DataProvider(parallel=true)
#16 Invocationcount=5
executes 5 time
Invocationcount=5,threadPoolSize=5
lauches browsers 5 time
^[0-9|\s]|[0-9|\s]$
chrome default profile
C:\Users\config\AppData\Local\Google\Chrome\User Data\Default
System.setProperty("webdriver.chrome.driver", "H:\\chromedriver (2).exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:\\Users\\config\\AppData\\Local\\Google\\Chrome\\User Data\\Default");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
driver.get("https://www.zoho.com/");
No comments:
Post a Comment