Using same browser instance how to run the test suite - selenium

I'm having a testsuite(4 classes) which have common login for all classes. When I run the test suite, with first class alone having login functionality and commenting the login code for rest of the 3 classes(redirecting the url ), its running only the first class, rest of the 3 classes got failed!
<classes>
<class name="testcases.TestClass1"></class>
<class name="testcases.TestClass2"></class>
<class name="testcases.TestClass3"></class>
<class name="testcases.TestClass4"></class>
</classes>
Please help on this

You can try using before suite annotation available in TestNG.
#BeforeSuite: The annotated method will be run before all tests in this suite have run.
Create the driver instance as part of before suite and access it across.
Refer to TestNG documentation for more information on the annotation.

Related

How to manage same selenium test for multiple user role

I have been developing a selenium/Java-based test automation in my company as a beginner. I am using the page object model and TestNG testing framework. Till now I have written test scripts with respect to the user (role: admin). Now I have to test the application functionality based on different users type e.g technician, support team, service assistance, and so on.
Some users have the same permissions in the application as admin users. E.g Test case: create an invoice (service assistance and service advisors also have permission to create invoices as admin). Test case script for creation of invoice I have already created but I don't want to create the same script again and again for each user role. So I want some suggestions on how can I manage this type of situation. If someone provides some documentation, or project example it will be great help for me. Also, I want to know how can I manage these types of test cases in different test suits.
I thought of some of the solutions:
Adding user role parameter in each test case/class
using TestNg Groups
You might do it in a next way:
1 Add role parameter to your test configuration when you authorize the user. It might be e.g #BeforeTest method. Annotate your method with #Parameter, and also set the value in testng.xml.
This tutorial may help:
https://www.toolsqa.com/testng/testng-parameters/
2 Create several TestNG xml suites. For a big project, I suggest creating a separate suite.xml per user role with all the test classes and packages you need. Then you'll be able to create the main TestNG xml and refer to other xmls with suite-file.
See https://stackoverflow.com/a/31851469/5226491
You can organize the suite to stay using a single xml, it depends on what you prefer.
How to group tests
I suggest do not mix test methods for multiple roles within a single class. Try to split them into separate classes.
This suggestion induces to keep test structure simple.
The main idea:
define the role before the test class run, and do not switch it when the class run is in progress.
Let's imagine:
you have roles A, B, C.
and test methods: method1Test, ...method7Test.
And
method1Test, method2Test, method3Test work for all roles.
method4Test, method5Test work for all roles A, B
method6Test, method7Test work for all roles B, C
So, this suggests to split this methods on 3 classes:
Class1Test with method1Test, method2Test, method3Test
Class2Test with method4Test, method5Test
Class3Test with method6Test, method7Test
Eventually, you'll have the ability to define the classes used per a unique role, and also to reuse some classes for multiple roles.
Example for single TestNG xml file.
<suite name="test-suite">
<test name="roleA-suite">
<parameter name="roleName" value="roleA"/>
<classes>
<class name="Class1Test"/>
<class name="Class2Test"/>
</classes>
</test>
<test name="roleB-suite">
<parameter name="roleName" value="roleB"/>
<classes>
<class name="Class1Test"/>
<class name="Class2Test"/>
<class name="Class3Test"/>
</classes>
</test>
<test name="roleC-suite">
<parameter name="roleName" value="roleC"/>
<classes>
<class name="Class1Test"/>
<class name="Class3Test"/>
</classes>
</test>
</suite>
Using Groups
It's also possible for this task, but I personally don't like using groups, IMHO they introduce a lot of complexity and it might be not easy to combine this with other TestNG features, e.g. with setting priority on tests, or with the depends-on feature. So maybe other users will share the positive experience using groups.
Anyway, you might try to use a groups approach and choose the best, you like.
Look at this tutorial:
https://www.lambdatest.com/blog/grouping-test-cases-in-testng/

Rerunned test method marked as SKIPPED

I wrote a script in selenium with TestNg framework such that the failed test method will be rerun, and this is at Test Level by indicating the location of the class that implements the IRetryAnalyzer in #Test()
TestNG had successfully rerun the failed test method; the output has the following information: for the first run, Failed Test Method is marked as Failed, and after rerunning it, the method is marked as skipped. What does it mean? Could anyone help me to interpret the result?
Here is the screenshot of the result:
There is an ongoing issue on the official project.
You're welcome to follow:
https://github.com/cbeust/testng/issues/878

Katalon - Executing test suite in a test suite setup method?

I'm trying to build a test hierarchy where other test suites are executed so a new test suite picks up where the last suite left off. Is there some way I can run a test suite execution in the set up of my test suite?
#SetUp(skipped = false)
def setUp() {
// execute test suite here
}
You can write to a file in a teardown method of the first test suite, and read from the same file in the setup method of the next test suite.
My first idea was to use Global Variables to do that, but they are relevant only inside of a single test suite.

I want to run each .feature file as single TestNG test using Karate?

I want to run each .feature file as single TestNG test using Karate, how can I do it?
We are using karate to automate REST API's. We have custom listener which will record status of each TestNG test and other information to postgress DB.
One more way running by tags:
#CucumberOptions(tags = { "#getVersion" })
public class GetVersionTest extends KarateRunner {
}
Feature File:
#getVersion
Feature: Testing Rest API with Karate and Java
Scenario:
Given url 'https://......'
When method get
Then status 200
And match response contains '{version= x.xx.x}'
Did you try using the Karate TestNG support ? The documentation has details on how to use: https://github.com/intuit/karate#running-with-testng
As the developer of Karate I actually strongly recommend that teams don't use TestNG. My suggestion is that you use the new 'hooks' feature to call your custom Java code from Karate directly and you won't depend on TestNG or even JUnit.
If you still need a custom way of running, the TestNG support is actually a single class: KarateRunner.java. I really don't know if it runs each feature as a TestNG test or not since I am not a TestNG expert. But you should be able to create your own version of this Java code that does what you want and maybe contribute it back to the project.

How do you get access to the current filtered trait in xUnit

In xUnit, is there a way to get access to the current trait filter during the test execution? For example, during our build process we setup a task to run our tests with a given trait (i.e. browser type). In our test setup code, I would like to know if the requested test run has received a trait filter, so I can use that to determine what Selenium web driver to use for that test run.
Thanks in advance for your assistance.