Selenium_testng_Java_passing data to both #beforetest and #test - java

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.

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;
}
}

Is there way to send different parameter values to different classes inside a test inside testng.xml

I need to pass a different parameter value for different classes inside a test. The following code gives me an error saying :
"The content of element type "classes" must match
'(class*,parameter*)'.".
Is there any other way I can pass the parameter value to be accessible by a class?
<suite name="Project">
<test thread-count="5" name="Test">
<classes>
<parameter name="URL" value="https://testRunner1.com" />
<class name="project.TestRunner1"/>
<parameter name="URL" value="https://testRunner2.com" />
<class name="project.TestRunner2"/>
</classes>
</test>
</suite>
There are two solutions.
1. Move the classes to different test as given below. Here you can have same name for the Parameters.
<suite name="Project">
<test thread-count="5" name="Test">
<classes>
<parameter name="URL" value="https://testRunner1.com" />
<class name="project.TestRunner1"/>
</classes>
</test>
<test thread-count="5" name="Test">
<parameter name="URL" value="https://testRunner2.com" />
<class name="project.TestRunner2"/>
</test>
</suite>
You need to change the parameter name in the class and change the xml as given below
I rewrote my code like below which worked.
<suite name="Project">
<test thread-count="5" name="Test">
<classes>
<class name="project.TestRunner1">
<parameter name="URL" value="https://testRunner1.com" />
</class>
<class name="project.TestRunner2">
<parameter name="URL" value="https://testRunner2.com" />
</class>
</classes>
</test>
</suite>

Is there a way to set global parameters in TestNG?

I'm currently automating web app using Selenium WebDriver framework together with TestNG. I want to provide parameters for each test classes within testing.xml file, but on the web resources, I've seen only approach where certain parameters are predefined to use for single classes like:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parameterization Test Suite">
<test name="Testing Parameterization">
<parameter name="browser" value="Firefox"/>
<parameter name="username" value="testuser"/>
<parameter name="password" value="testpassword"/>
<classes>
<class name="com.parameterization.TestParameters" />
</classes>
</test>
</suite>
where the class name, clearly states only one TestParameters class.
Is there any way to set some parameters right there as global ones? So we can use them in any test class within the project?
I'm not really sure if I understand your question, but you can define parameters on suite level (for each test, in the example suite-param), or on test level:
As taken from https://howtodoinjava.com/testng/testng-test-parameters-through-testng-xml-and-parameters-annotation/
<suite name="Parameter test Suite" verbose="1">
<!-- This parameter will be passed to every test in this suite -->
<parameter name="suite-param" value="suite level parameter" />
<test name="Parameter Test one">
<classes>
<class name="com.howtodoinjava.test.ParameterTest">
<methods>
<include name="prameterTestOne" />
</methods>
</class>
</classes>
</test>
<test name="Parameter Test two">
<!-- This parameter will be passed this test only -->
<parameter name="test-two-param" value="Test two parameter" />
<classes>
<class name="com.howtodoinjava.test.ParameterTest">
<methods>
<include name="prameterTestTwo" />
</methods>
</class>
</classes>
</test>
<test name="Parameter Test three">
<!-- Overriding suite level parameter -->
<parameter name="suite-param" value="overiding suite parameter" />
<!-- Test specific parameter -->
<parameter name="test-three-param" value="test three parameter" />
<classes>
<class name="com.howtodoinjava.test.ParameterTest">
<methods>
<include name="prameterTestThree" />
</methods>
</class>
</classes>
</test>
</suite>

Referring test groups in testng.xml

Let's say I've annotated my test class with a test group in TestNG:
#Test(group='smoke-tests')
public class CheckEnvironmentTest {
...
}
Is there a way to refer to this group in testng.xml? Something like this (if it was implemented):
<suite name="Suite1" verbose="1" >
<test name="Sequential" parallel="false" >
<test-group ref="smoke-tests" />
</test>
<test name="ParallelGroup" parallel="classes" >
<test-group ref="regular-tests" />
</test>
</suite>
You might want to do something like this
<test name="sample">
<groups>
<run>
<include name="smoke-tests"/>
</run>
</groups>
<classes>
<class name="CheckEnvironmentTest"/>
</classes>
</test>

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>

Categories