Add cross browser for multiple packages using TestNG and Java - java

I am using xml file to run all my packages of a project parallel, how to add cross browser to this xml file:
<suite name="Web Smoke Tests" verbose="1" preserve-order="true"
parallel="tests" thread-count="4">
<test name="Test2">
<classes>
<class name="package.class2"></class>
</classes>
</test>
<test name="Test1">
<classes>
<class name="package.class1"></class>
</classes>
</test>
<test name="Test">
<classes>
<class name="package.class"></class>
</classes>
</test>

You can refer the following sample for achieving this:
Modify your xml file based on the below sample:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel" thread-count="4" parallel="tests">
<test name="SingleTestChrome">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="chrome"/>
<classes>
<class name="com.browserstack.SingleTest"/>
</classes>
</test>
<test name="SingleTestFirefox">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="firefox"/>
<classes>
<class name="com.browserstack.SingleTest"/>
</classes>
</test>
<test name="SingleTestSafari">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="safari"/>
<classes>
<class name="com.browserstack.SingleTest"/>
</classes>
</test>
<test name="SingleTestIE">
<parameter name="config" value="parallel.conf.json"/>
<parameter name="environment" value="ie"/>
<classes>
<class name="com.browserstack.SingleTest"/>
</classes>
</test>
</suite>
Sample Configuration file:
{
"server": "hub-cloud.browserstack.com",
"user": "BROWSERSTACK_USERNAME",
"key": "BROWSERSTACK_ACCESS_KEY",
"capabilities": {
"build": "testng-browserstack",
"name": "parallel_test",
"browserstack.debug": true
},
"environments": {
"chrome": {
"browser": "chrome"
},
"firefox": {
"browser": "firefox"
},
"safari": {
"browser": "safari"
},
"ie": {
"browser": "internet explorer"
}
}
}
Reference: https://github.com/browserstack/testng-browserstack

Related

Managing memory using TestNG framework while running selenium webdriver testings

I encounter memory issues where the automation suite which I am running is stuck at some point in a way that I cannot explain or understand, I am using TestNG framework to run my automation test suite the test is using 10 threads (configured in testng.xml file with selenium 3) and the test itself is running of jenkins (I am using docker to run the project), this is my testng.xml setup:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Automation_Suite" parallel="tests" thread-count="10">
<!-- ================================CMS-PART-START=====================================-->
<test name="Categories Test Happy Flow" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.categories.CategoriesTestHappyFlow"/>
</classes>
</test>
<test name="Categories Test Negative Flow" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.categories.CategoriesTestNegativeFlow"/>
</classes>
</test>
<test name="Icon Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.content_manager.icons.ContentManagerIconTest"/>
</classes>
</test>
<test name="Presentation Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.content_manager.presentations.ContentManagerPresentationTest"/>
</classes>
</test>
<test name="Video Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.content_manager.videos.ContentManagerVideoTest"/>
</classes>
</test>
<test name="Cyberpedia Categories Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.pedia.PediaCategoriesTest"/>
</classes>
</test>
<test name="Quizzes Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.pedia.QuizzesTest"/>
</classes>
</test>
<test name="Terms Test Happy Flow" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.pedia.TermsTestHappyFlow"/>
</classes>
</test>
<test name="Terms Test Negative Flow" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.pedia.TermsTestNegativeFlow"/>
</classes>
</test>
<!-- <!– ===========================================================================–>-->
<test name="Classes Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.institutions.ClassesTest"/>
</classes>
</test>
<test name="College Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.institutions.CollegeTest"/>
</classes>
</test>
<test name="Institution Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.institutions.InstitutionTest"/>
</classes>
</test>
<!-- ===========================================================================-->
<test name="Char Limitations Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.practice_arena.lessons.CharactersLimitationTest"/><!--verify there are no lessons before running this test-->
</classes>
</test>
<test name="General Information Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.practice_arena.lessons.EditPracticeArenaLessonGeneralInformationTest"/><!--verify there are no lessons before running this test-->
</classes>
</test>
<test name="Lesson Materials Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.practice_arena.lessons.EditPracticeArenaLessonLessonMaterialsTest"/>
</classes>
</test>
<test name="Module Creation Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.practice_arena.modules.PracticeArenaCreateNewModuleTest"/>
</classes>
</test>
<test name="Lesson Assigned Modules Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.practice_arena.modules.PracticeArenaLessonAssignedModulesTest"/>
</classes>
</test>
<test name="Lesson List Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.practice_arena.lessons.PracticeArenaLessonListTest"/>
</classes>
</test>
<test name="Module Skills Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.practice_arena.modules.PracticeArenaModuleSkillsTest"/>
</classes>
</test>
<test name="Module List Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.practice_arena.modules.PracticeArenaModulesListTest"/>
</classes>
</test>
<test name="Upload Lesson Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.practice_arena.lessons.UploadNewPALessonTest"/>
</classes>
</test>
<!-- ===========================================================================-->
<test name="Create syllabus Happy Flow Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.syllabi.CreateSyllabiHappyFlowTest"/>
</classes>
</test>
<test name="Create syllabus Negative Flow Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.syllabi.CreateSyllabiNegativeFlowTest"/>
</classes>
</test>
<test name="Create Complex User Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.users.ComplexCreateUserTest"/>
</classes>
</test>
<test name="Create Admin User Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.users.CreateAdminTest"/>
</classes>
</test>
<test name="Create Candidate User Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.users.CreateCandidateTest"/>
</classes>
</test>
<test name="Create Demo User Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.users.CreateDemoTest"/>
</classes>
</test>
<!-- <test name="Create Master User Test" parallel="false">-->
<!-- <parameter name="username" value=""/>-->
<!-- <classes>-->
<!-- <class name="com.hackeruso.automation.ui.cms.users.CreateMasterTest"/>-->
<!-- </classes>-->
<!-- </test>-->
<test name="Create Student User Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.users.CreateStudentTest"/>
</classes>
</test>
<test name="Create Teacher User Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.users.CreateTeacherTest"/>
</classes>
</test>
<test name="Edit Student Panel Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.users.EditStudentPanelTest"/>
</classes>
</test>
<test name="Edit User Panel Test" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.users.EditUserPanelTest"/>
</classes>
</test>
<test name="Prod Sanity - Reports in CMS section" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.cms.users.ProdSanityReportsCMSPageTest"/>
</classes>
</test>
<!-- ================================CMS-PART-END=====================================-->
<!-- ================================WEBSITE-PART-START=====================================-->
<test name="Prod Sanity - Every Tab is Accessible - Website section" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.website.home.ProdSanityEveryTabAccessibleTest"/>
</classes>
</test>
<test name="Prod Sanity - Post Support Test - Website section" parallel="false">
<parameter name="username" value=""/>
<classes>
<class name="com.hackeruso.automation.ui.website.support.PostLoginSupportTest"/>
</classes>
</test>
</suite>
and I am using docker compose file to run the automation suite on top of it and this is the docker compose file:
version: '3'
services:
cywar-test-automation:
image: markhobson/maven-chrome:jdk-11
stop_signal: SIGKILL
stdin_open: true
tty: true
privileged: true
user: root
working_dir: /opt/neo
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./:/opt/neo
- /dev/shm:/dev/shm
command: mvn clean test -Pregression
environment:
- MAVEN_OPTS=-Xms24g -Xmx24g
When I am running the automation suite I see that not all chrome processes are being closed after the test is over although I am using 'driver.quit'
this is how my webdriver initialization looks like:
public class BaseUiTest extends BaseApiTest {
private final Browser BROWSER = Browser.valueOf(EnvConf.getProperty("ui.browser.type"));
protected String ADMIN_USERNAME;
protected final String ADMIN_PASSWORD = EnvConf.getProperty("hackeruso.admin.user.password");
protected DriverFactory driver;
protected LoginPage loginPage;
protected CmsPage cmsPage;
protected UpperManagementToolBar upperManagementToolBar;
protected WebSiteCommonPage webSiteCommonPage;
#BeforeClass(alwaysRun = true)
#Parameters({"username"})
public final void BaseUiSetup(ITestContext context, #Optional String username){
if(username!=null) {
this.ADMIN_USERNAME = username; //For running tests using maven command with testng xml file
}else{
this.ADMIN_USERNAME = EnvConf.getProperty("hackeruso.admin.user.email"); //For running tests locally from IDE
}
open();
}
protected void open() {
driver = DriverFactory.open(BROWSER, TEST_DOWNLOAD_FOLDER);
loginPage = new LoginPage(driver);
cmsPage = new CmsPage(driver);
webSiteCommonPage = new WebSiteCommonPage(driver);
upperManagementToolBar = new UpperManagementToolBar(driver);
}
protected void navigateTo(String url) {
driver.get(url);
Log.i("Successfully navigated to =[%s]", url);
}
#AfterClass(alwaysRun = true)
public final void baseTeardown() {
Date testEndTime = new Date();
if (driver != null) {
printBrowserLog();
driver.quit();
}
Log.i("testEndTime=[%s]", testEndTime);
}
public DriverFactory getDriver() {
return driver;
}
}

I want to create xml in order to execute my classes

I have created a TestNG XML File, which I have called multiple classes. My concern is that I want to execute my test cases on all classes which I have arranged from top to bottom like below. However some of the bottom cases depend on upper cases, and whenever I run my XML File it selects the classes randomly. How would I go about doing this?
Type - 1
{
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite3">
<test name="Test3" preserve-order="true">
<classes>
<class name="com.netgearInsight.testReports.RegistrationTestCase"/>
<class name="com.netgearInsight.testReports.LoginTestCase"/>
<class name="com.netgearInsight.testReports.AddNetworkTestCase"/>
<class name="com.netgearInsight.testReports.AddDevicesTestCase"/>
<class name="com.netgearInsight.testReports.CreateSsidTestCase"/>
<class name="com.netgearInsight.testReports.DeleteDevicesTestCase"/>
<class name="com.netgearInsight.accountCheck.About"/>
<class name="com.netgearInsight.accountCheck.AccountManage"/>
<class name="com.netgearInsight.testRunEnd.GetTotalStatus"/>
<class name="com.netgearInsight.testRunEnd.SendReport"/>
</classes>
</test> <!-- Test3 -->
</suite> <!-- Suite3 -->
}
Type - 2
{
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1">
<test thread-count="5" name="Test One" >
<classes>
<class name="com.netgearInsight.testReports.RegistrationTestCase"/>
<class name="com.netgearInsight.testReports.LoginTestCase"/>
</classes>
</test> <!-- Test -->
<test thread-count="5" name="Test Two" >
<classes>
<class name="com.netgearInsight.testReports.AddNetworkTestCase"/>
<class name="com.netgearInsight.testReports.AddDevicesTestCase"/>
<class name="com.netgearInsight.testReports.CreateSsidTestCase"/>
</classes>
</test> <!-- Test -->
<test thread-count="5" name="Test Three" >
<classes>
<class name="com.netgearInsight.testReports.UrlFilterTestCase"/>
<class name="com.netgearInsight.testReports.DeleteDomainTestCase"/>
<class name="com.netgearInsight.testReports.DeleteDevicesTestCase"/>
</classes>
</test> <!-- Test -->
<test thread-count="5" name="Test Four" >
<classes>
<class name="com.netgearInsight.testReports.AddDataVLAN"/>
<class name="com.netgearInsight.accountCheck.AccountManage"/>
<class name="com.netgearInsight.accountCheck.About"/>
<class name="com.netgearInsight.accountCheck.ChangePassword"/>
</classes>
</test> <!-- Test -->
<test thread-count="5" name="Test Five">
<classes>
<class name="com.netgearInsight.testRunEnd.GetTotalStatus"/>
<class name="com.netgearInsight.testRunEnd.SendReport"/>
</classes>
</test> <!-- Test -->`enter code here`
</suite> <!-- Suite -->
}

Selenium_testng_Java_passing data to both #beforetest and #test

I have my TesTng class with 3 test (A,B, C) and this class extends base class which has #beforemethod and # aftermethod
Now I want to pass browser to # before method and email to method A
Below is my sample data.
Email has to be unique every time.
one of the ways is to use #Parameters annotation.
Code for #BeforeMethod is-
#BeforeMethod
#Parameters("browser")
public void testMethod1(String browser) {
//do your task here
}
Code for method A-
#Test
#Parameters("email")
public void A(String email) {
//implement your test logic here
}
Sample TestNG Sample-
<suite name="Suite1" verbose="1" >
<test name="Test1">
<parameter name="browser" value="firefox"/>
<parameter name="email" value="an-email-id"/>
<classes>
<class name="packagename.ClassName"/>
</classes>
</test>
</suite>
I found a solution for my problem please le me know if more efficient way is there.. I have userd # parameter (thanx optimist_creeper) and In testng.xml I have created different test
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Chrome_test">
<parameter name="Browser" value="chrome"></parameter>
<parameter name="email" value="a"></parameter>
<classes>
<class name="selenium.mail.gmail.BabyTest">
<methods>
<include name="A" />
<include name="B" />
<exclude name="C" />
</methods>
</class>
</classes>
</test> <!-- Test -->
<test name="FF_test">
<parameter name="Browser" value="Firefox"></parameter>
<parameter name="email" value="b"></parameter>
<classes>
<class name="selenium.mail.gmail.BabyTest">
<methods>
<exclude name="C" />
</methods>
</class>
</classes>
</test>
<test name="IE_test">
<parameter name="Browser" value="IE"></parameter>
<parameter name="email" value="c"></parameter>
<classes>
<class name="selenium.mail.gmail.BabyTest">
<methods>
<include name="A" />
</methods>
</class>
</classes>
</test>
sorry for wrong indentation, code with correct indentation wasn't displayed properly.

Unable to run the Regression group in testng.xml file for different browsers

I have configured the testng.xml file to run the Regression group in different browsers.Below is the testng.xml code for the same.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SeleniumSuite" verbose ="1" thread-count = "1" parallel="false">
<**test** name="FirefoxTest">
<groups>
<run>
<include name="Regression"></include>
</run>
</groups>
<parameter name="browser" value="firefox" />
<classes>
<class name="Testscript.Program111_RediffLogin" />
</classes>
</test>
<test name="IETest">
<groups>
<run>
<include name="Regression"></include>
</run>
</groups>
<parameter name="browser" value="ie" />
<classes>
<class name="Testscript.Program111_RediffLogin" />
</classes>
</test>
</suite>
when I hover the mouse on the tag, it displays an error message as "The content of element type "test" must match "(method-selectors?,parameter*,groups?,packages?,classes?)".At the Test class level I have defined all the parameters properly for the regression test to run.But still I am seeing the error in the testng.xml file.Can any one of you look into this and help me!
Please find the test case I am using for the automation
#Test(groups={"Regression"},dataProvider = "hashmapdataprovider",dataProviderClass =Dataprovider.Dataprovider_Hashmap.class,priority=1 )
public void validLogin(Map<String,String> hm) throws IOException
{
pageobjects.Signin();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
OriginalExcelRW Excel = new OriginalExcelRW("F:\\anand_acer\\selenium\\rediffbooks.xlsx");
XSSFSheet s1 = Excel.Setsheet("Sheet1");
SoftAssert s_assert = new SoftAssert();
if (hm.get("Executionflow").contains("anand"))
{
pageobjects.Username1(hm.get(Excel.Readvalue(s1, 0, 2)));
pageobjects.pass1(hm.get(Excel.Readvalue(s1, 0, 3)));
//s_assert.assertEquals(hm.get(Excel.Readvalue(s1, 0, 2)), hm.get(Excel.Readvalue(s1, 0, 3)), "both the usssser Ideee and password doesnt matches");
//logger.info("Usssser Ideeee");
pageobjects.login();
s_assert.assertTrue(true, "login success");
//logger.info("The login was success");
System.out.println("Valid login is passed");
pageobjects.signout();
pageobjects.Signin();
//pageobjects.cleartext();
}
s_assert.assertAll();
}
Looks like from the error message you have posted they must be in a special order of indentation?
Here is an example of my XML which works
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite verbose="1" name="example suite 1">
<listeners>
<listener class-name="ReportListener.ReportListenerClass" />
</listeners>
<test name="Logout tests">
<classes>
<class name="com.emc.qe.u360.tests.LogoutTests" />
</classes>
</test>
<test name="Login tests">
<classes>
<class name="com.emc.qe.u360.tests.LoginPageTests" />
</classes>
</test>
</suite>

TestNG:Cannot find class in classpath, testng.xml

This is my testng.xml file, i named it differently, but it should still work:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TEST_LINKS">
<test name="TECT на подбор спеца">
<classes>
<class name="Podbor_specalista_url_click_test" />
</classes>
</test>
<test name="TECT на клик блога">
<classes>
<class name="Blog_url_click_test" />
</classes>
</test>
</suite>
Now, i get the error, but where do I fix it?
i want to run the whole test suite consisting of these tests:
You need to give the fully qualified class name of the class you want to test in class tag, something like this
<suite thread-count="5" name="Ant suite" junit="false" annotations="JDK">
<test name="Ant test">
<classes>
<class name="com.sample.test.MyUnitTest"/>
</classes>
</test>
</suite>

Categories