How to use groups in testNG - selenium

I am implementing groups in the testng test suite.When I run my testng suite suite,it runs only the 'FireFox' test and when I put the tag in 'Firefox' test
it runs the 'IE' test.Kindly let me know what can be the reason for this??
<groups>
<run>
<include = "smoke"/>
</run>
</groups>
<classes>
<class name = "com.asw.beginner.tests.NewTest"/>
</classes>
</test>
<test name = "FireFox" allow-return-values = "true">
<parameter name = "browser" value = "FF"/>
<classes>
<class name = "com.asw.beginner.tests.NewTest"/>
</classes>
</test>

Define your test as following:
public class Test1 {
#Test(groups = { "functest" })
public void testMethod1() {
}
#Test(groups = {"functest", "checkintest"} )
public void testMethod2() {
}
#Test(groups = { "checkintest" })
public void testMethod3() {
}
}
Then you can use xml file as following
<test name="Test1">
<groups>
<run>
<include name="functest"/>
</run>
</groups>
<classes>
<class name="example1.Test1"/>
</classes>
</test>
Above test will only run the method with group functest.
You can read more about testng from Official testng documatation

Following is example java code
import org.testng.Assert; import org.testng.annotations.Test;
public class GroupTestExample { String message = ".com"; MessageUtil messageUtil = new MessageUtil(message);
#Test(groups = { "functest", "checkintest" })
public void testPrintMessage() {
System.out.println("Inside testPrintMessage()");
message = ".com";
Assert.assertEquals(message, messageUtil.printMessage()); }
#Test(groups = { "checkintest" })
public void testSalutationMessage() {
System.out.println("Inside testSalutationMessage()");
message = "tutorialspoint" + ".com";
Assert.assertEquals(message, messageUtil.salutationMessage()); }
#Test(groups = { "functest" })
public void testingExitMessage() {
System.out.println("Inside testExitMessage()");
message = "www." + "tutorialspoint"+".com";
Assert.assertEquals(message, messageUtil.exitMessage()); } }
following is Testng.xml file
<groups>
<run>
<include name = "functest" />
</run>
</groups>
<classes>
<class name = "GroupTestExample" />
</classes>
</test> </suite>

Related

Android kotlin preferences spinner with values from arrays.xml

settings_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/settings"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
root_preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:dialogTitle="Select bluetooth adapter"
android:key="bluetoothName"
android:summary="Click to show a list to choose from"
android:title="Bluetooth adapter" />
<ListPreference
android:dialogTitle="Select units"
android:entries="#array/units_names"
android:entryValues="#array/units_values"
android:key="units"
android:summary="Click to show a list to choose from"
android:title="Units" />
</PreferenceScreen>
SettingsActivity.kt
class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.settings_activity)
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, SettingsFragment())
.commit()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
}
}
}
arrays.xml
<resources>
<string-array name="units_values">
<item>miles</item>
<item>km</item>
</string-array>
<string-array name="units_names">
<item>miles and mpg</item>
<item>km and l/100km</item>
</string-array>
</resources>
MainActivity.kt
...
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.toolbar, menu);
return true;
}
override fun onOptionsItemSelected( item: MenuItem) : Boolean {
when (item.itemId) {
R.id.toolbar_item_settings -> {
val intent: Intent = Intent(this, SettingsActivity::class.java)
startActivity(intent)
return true
}
}
return true
}
...
Why are the array values not showing in the spinner?
It's because the list item text colour has defaulted to black.
It needs:
<item name="textColorAlertDialogListItem">#color/text</item>

Can we pass parameters or any attributes at method level in testng.xml?

// Testng xml
<test name="Test1" preserve-order ="true">
<parameter name="deviceName_" value="aaaa"/>
<classes>
<class name="Test">
<methods>
<include name="methodName"/>
</methods>
</class>
</classes>
</test>
// Can i pass parameters at method level like i passed deviceName at test level
Actually, parameters on method level exists. Here is an example:
<suite name="my-suite" verbose="1">
<test name="my-test">
<classes>
<class name="testng.ex1.TestParams">
<methods>
<include name="m1">
<parameter name="key1" value="val1"/>
<parameter name="key2" value="val2"/>
</include>
<include name="m2">
<parameter name="key1" value="valA"/>
<parameter name="key2" value="valB"/>
</include>
</methods>
</class>
</classes>
</test>
</suite>
and the test class is,
package testng.ex1;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestParams {
#Test
#Parameters({ "key1", "key2" })
public void m1(String key1, String key2) throws Exception {
System.out.println(key1 + ", " + key2);
}
#Test
#Parameters({ "key1", "key2" })
public void m2(String key1, String key2) throws Exception {
System.out.println(key1 + ", " + key2);
}
}
another approach is to use a data-provider that fetches the keys from testng.xml. See example:
<suite name="my-suite" verbose="1">
<test name="my-test">
<classes>
<parameter name="keys" value="key1,key2,key3,key4" />
<class name="testng.ex2.TestParams" />
</classes>
</test>
</suite>
The test calss,
package testng.ex2;
import java.util.Arrays;
import java.util.List;
import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestParams {
#Test(dataProvider = "dp")
public void m1(Employee e) throws Exception {
System.out.println("name: " + e.getName() + ", age: " + e.getAge());
}
#DataProvider(name = "dp")
#Parameters("keys")
public Object[][] createData(ITestContext ctx) {
String keysString = ctx.getCurrentXmlTest().getLocalParameters().get("keys");
List<String> keys = Arrays.asList(keysString.split(","));
Object[][] result = new Object[keys.size()][1];
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
result[i] = new Object[] { new Employee(key) };
}
return result;
}
}
The employee class,
package testng.ex2;
public class Employee {
private final String name;
private final int age;
public Employee(String key) {
// use 'key' to lookup employee in database
name = key + "_name"; // dummy value
age = 41; // dummy value
}
String getName() {
return name;
}
int getAge() {
return age;
}
}
you can get the parameter in the before method as given below,
#BeforeMethod
#Parameters({ "key1", "key2" })
public void beforem1(String key1, String key2){
System.out.println(key1 + ", " + key2);
}

Re-Running TestNG tests not working for all the Tests

I am using IRetryAnalyzer interface to run failed test cases again. It works fine if I have only one Test in my TestNG XML file, however if I have multiple tests in my XML file it works only for the first test.
Root Cause: the retryCount for second test is set to 1, I am not sure how to reset it.
Existing open issue:
https://github.com/cbeust/testng/issues/1241
RetryAnalyzer.java
public class RetryAnalyzer implements IRetryAnalyzer {
private int retryCount = 0;
private int maxRetryCount = 1;
public boolean retry(ITestResult result) {
if (retryCount < maxRetryCount) {
retryCount++;
return true;
}
return false;
}
}
AnnotationTransformer.java
public class AnnotationTransformer implements IAnnotationTransformer {
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
annotation.setRetryAnalyzer(RetryAnalyzer.class); //to enable for all the methods.
}
}
TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="BrowserStack" >
<listeners>
<listener class-name="uk.co.gaurang.libs.ResultListener" />
<listener class-name="uk.co.gaurang.libs.AnnotationTransformer" />
</listeners>
<test name="IPhone6S">
<classes>
<class name="uk.co.gaurang.tests.Demo"/>
</classes>
</test>
<test name="IPhone6SPlus">
<classes>
<class name="uk.co.gaurang.tests.Demo"/>
</classes>
</test>
</suite>

I am getting null pointer exception while running testng code through XML.But when running code directly its working fine

Code is Grouping in TESTNG
package testNG_annot;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TestNG_grp1
{
#BeforeMethod
public void CarBM() {
System.out.println("CarBM");
}
*** // Created group car and Two wheeler//***
#Test(groups = { "Car" })
public void Sedan1() {
System.out.println("Test1-Verna");
}
#Test(groups = { "Car" })
public void Sedan2() {
System.out.println("Test2-BMW");
}
#Test(groups = { "TwoWheeler" })
public void Scooter1() {
System.out.println("Test3-ScootyPep");
}
#Test(groups = { "TwoWheeler" })
public void aScooter2() {
System.out.println("Test4-TVS");
}
}
suite.xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="grp11">
<test name="group1">
<gropus>
<run>
<include name="Car"/>
</run>
</gropus>
<classes>
<class name="testNG_annot.TestNG_grp1"/>
</classes>
</test>
</suite>
Your spelling for groups tag is wrong. Change the spelling and run it again.
<groups>
<run>
<include name="Car"/>
</run>
</groups>
Form the testng.xml file as below format. Run it as testng test and you will get your test running.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="Test" >
<classes>
<class name="testNG_annot.TestNG_grp1" />
</classes>
</test>
</suite>
Keep this on top in your xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

How to run TestNG tests one-by-one

I have a test suite and I want to run tests form XML file one-by-one. But when I'm running it, it runs all TCs at the same time (I have 3 open browsers at the same time).
My XML:
<suite name="TestingSuite" preserve-order="true" parallel="false" verbose="10">
<test name="Test1">
<classes>
<class name="guiAndFunctianal.LoginFail" />
<class name="guiAndFunctianal.LoginAsManager" />
<class name="guiAndFunctianal.CreateUserTest" />
</classes>
</test>
</suite>
My TCs looks like this:
public class LoginFail extends AbstractTest{
# BeforeTest
public void openBrowser() {
openBrowserFireFoxAllTcs();
}
# Test
public void main (){
}
# AfterTest
public void quit() {
driver.quit(); }
AbstractTest
public class AbstractTest {
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 10);
public void openBrowserFireFoxAllTcs() {
driver.get("some URL");
}
I assume you want sequential execution. Open a browser>finish testing>close browser and then repeat the same for other browsers. If that's case I would do the following:
TestNG.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" junit="false" parallel="false" configfailurepolicy="skip" thread-count="5" skipfailedinvocationcounts="false" data-provider-thread-count="10" group-by-instances="false" preserve-order="true" allow-return-values="false">
<test name="firefox" junit="false" skipfailedinvocationcounts="false" preserve-order="true" group-by-instances="false" allow-return-values="false">
<parameter name="browser" value="firefox" />
<classes>
<class name="com.github.tests.GitHubHomePageTests" />
</classes>
</test>
<test name="ie" junit="false" skipfailedinvocationcounts="false" preserve-order="true" group-by-instances="false" allow-return-values="false">
<parameter name="browser" value="ie" />
<classes>
<class name="com.github.tests.GitHubHomePageTests" />
</classes>
</test>
<test name="chrome" junit="false" skipfailedinvocationcounts="false" preserve-order="true" group-by-instances="false" allow-return-values="false">
<parameter name="browser" value="chrome" />
<classes>
<class name="com.github.tests.GitHubHomePageTests" />
</classes>
</test>
</suite>
How I instantiate the drivers:
package com.github.tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.*;
public class BaseTest {
public WebDriver driver;
String url = "https://github.com/";
#Parameters("browser")
#BeforeClass
public void SetUpTests(String browser) {
if (browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("ie")) {
System.setProperty("webdriver.ie.driver", ".\\drivers\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
} else if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
}
//Navigate to url
driver.navigate().to(url);
//Maximize the browser window
driver.manage().window().maximize();
}
#AfterClass
public void CleanUpDriver() throws Exception {
// Quit current driver instance.
try {
driver.quit();
} catch (Exception ex) {
throw ex;
}
}
How the test run
package com.github.tests;
import com.github.pageobjects.GitHubLandingPage;
import org.testng.Assert;
import org.testng.annotations.Test;
public class GitHubHomePageTests extends BaseTest {#Test
public void ExploreLinkTest() {
String explorePageTitle = new GitHubLandingPage(driver)
.clickGitHubExplorePage()
.getGitHubExplorerPageTitle().trim();
System.out.println(explorePageTitle);
Assert.assertEquals(explorePageTitle, "Explore GitHub");
}
}
A global Github repo is available here
Try this:
<suite name="TestingSuite" preserve-order="true" thread-count="1" verbose="10">
<test name="Test1">
<classes>
<class name="guiAndFunctianal.LoginFail" />
</classes>
</test>
<test name="Test2">
<classes>
<class name="guiAndFunctianal.LoginAsManager" />
</classes>
</test>
<test name="Test3">
<classes>
<class name="guiAndFunctianal.CreateUserTest" />
</classes>
</test>
</suite>
Close your driver before quit:
# AfterTest
public void quit() {
driver.close()
driver.quit();
}