Cucumber Selenium - parallel browser testing - selenium

I have the following feature file :
Given I open "google.com" simultaneously in both FF and IE
When I type "stackoverflow" and submit
Then I should see the desired results
How can I run the test on 2 different browsers in parallel ?
I know it can done using TestNG, but I am not using TestNG in my project. I was wondering if there was some other approach.

I can think of three different different approaches here.
Write the scenario as you have done. When you find IE in the first step, create an IE instance. When you see FF in the first step, create a FF instance. Then use both in the following steps.
Do not include the browser at all in the steps. Create them and use them in the helper class you will delegate the work to.
Create one scenario for each browser. “When I open Google with Firefox…”
If you want to be explicit, use the last approach.
If you your users doesn’t care about the browsers, use the second approach.
I wouldn’t use the first approach myself.

Identify which approach is best to implement parallel execution - Cucumber-JVM 4 supports parallel execution from cucumber v 4.0.0 and we do not need to create individual runner per feature file and You can implement this with JUnit (Do not need to use TestNG & cucumber-jvm-parallel-plugin)
Steps to implement parallel execution starting from cucumber 4.0.0 -
1.Adding correct set of dependency. I have followed JUnit during the implementation.
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>4.2.3</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>datatable</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
2.Adding Maven-Surefire-Plugin under POM.XML
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<parallel>methods</parallel>
<threadCount>1</threadCount>
<reuserForks>false</reuserForks>
<testErrorIgnore>true</testErrorIgnore>
<testFailureIgnore>true</testFailureIgnore>
<includes>
<include>**/*RunCukeTest.java</include>
</includes>
</configuration>
</plugin>
Note - RunCukeTest is the runner file name and remember, TestNG dependency causes Surefire to ignore JUnit wrapper class. Remove all the TestNG dependencies if not required at all or you would need to define 2 execution one for TestNG & other for JUnit and disable one as per your need.
Once everything is done then you would need to pass browser name you want to run from a source like excel, json etc for each scenario.

If you are using cucumber you have to create multiple runners and then use maven to run them in parallel. So creating runner for every step definition is pain, so we have to create runners in run time in target folder.There are two ways to create runners on run time.
Use cucumber JVM parallel plugin here
If you are using the newest version of cucumber there is an awesome plugin called cucable
Here

Related

How to get the TestNG report in VS Code?

Running selenium test cases with TestNG in VS Code. But the test-output folder is not generated. Tried refreshing the project.
Unlike other IDEs, like Eclipse, etc., VS Code doesn't have a plugin for exclusive TestNG activities. In your case, to generate the TestNG report, you need to have the reporting dependency in your pom.xml:
<dependency>
<groupId>org.testng</groupId>
<artifactId>reportng</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>

JUnit on IntelliJ not working

I tried setting up JUnit 5 on my INtelliJ IDEA Community Edition 2018.2. The jar was downloaded but I am getting Cannot resolve symbol Assertions on importing
import static org.junit.jupiter.api.Assertions.*;
Error
Are you trying to use the JUnit assertions in a regular app class rather than a test class?
Delete <scope>test</scope>
When a Maven dependency carries a scope element with a value of test, that means you cannot use that library outside of your test-specific source package/folder.
If you are trying to call JUnit from code in your example project’s src/main/java/… folder hierarchy, you will see that error. If you call JUnit from src/test/java…, you will see success.
To enable JUnit in the src/main/java/… folder hierarchy, delete the scope element in your POM dependency. So this:
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.4.0-RC1</version>
<scope>test</scope>
</dependency>
…becomes this:
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.4.0-RC1</version>
</dependency>
By the way, note that as of 5.4.0 of JUnit, we can specify the new and very convenient single Maven artifact of junit-jupiter which in turn will supply 8 libraries to your project.

IntelliJ doesn't autocomplete JUnit 5 Jupiter classes

I have Maven imports for JUnit 5 in an IntelliJ install, and I have invalidated the cache and restarted.
IntelliJ will refuse to autocomplete (CTRL-SPACE) any class nor any static method belonging to classes in the org.junit.jupiter.api. package, unless the class' fully qualified name is spelled out. Even when writing the package for which the class belongs, will not suggest the classes of that package.
IntelliJ version is 2017.2.1. Jupiter version is 5.0.0-RC2, platform is 1.0.0-RC2.
I am unsure how to move on from here. How come this package is the only one that seems to refuse basic completion?
<properties>
<junit.jupiter.version>5.0.0-RC2</junit.jupiter.version>
<junit.platform.version>1.0.0-RC2</junit.platform.version>
</properties>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<!-- Only required to run tests in an IDE that bundles an older version -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<!-- Only required to run tests in an IDE that bundles an older version -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
Please check those classes are not in exclude list in File | Settings | Editor | General | Auto Import
You can try to downgrade JUnit 5 to M4 instead of RC2. IntelliJ 2017.2 is based on JUnit 5 M4, maybe that is causing the problem you are facing.

Need (Selenium webdriver + Junit) Sample test frame work code or the way how to run

I need a Selenium webdriver + Junit Sample test frame work.
where i can run the below things
1) Frame work should read the inputs/testcase id from config file/txt file
2) flexibility to run particular test case only when it fails.
3) Can be automated.
Currently my test cases are large number, if i use Junit TestSuite then more number of classes needs to be maintained, if i use Junit normal test case then i cant automate & cant run particular fail test case
Please help
#Test
public void test() {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com);
Assert.assertTrue(driver.getTitle().equals("Google"));
}
That's it, easist test you can get. I would also recommend Maven for handling your dependencies, in this case your pom.xml would look like
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.41.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

run maven tests from classpath

In order to clean up something of a giant mess, I set out to put the code of my tests all in one ordinary java project (all in src/main/java), and then declare that as a <scope>test</scope> dependency in another project, and expect the tests to run.
No such luck. surefire wants to just run the tests that it can see in the sources.
I can see a sadly obvious solution here involving the build-helper-plugin and adding the tests into the test compilation environment as a source directory, but I was hoping to avoid it.
In case anyone is wondering, the reason for all this is that the POM configuration for use of the failsafe plugin to run some integration tests got so complex that I wanted to split out the compiling of the test classes from the running of the tests.
This is now possible with Maven Surefire v2.15. Simply add the following kind of configuration to the surefire plugin:
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<configuration>
<dependenciesToScan>
<dependency>com.group.id:my-artifact</dependency>
<dependency>com.group.id:my-other-artifact</dependency>
</dependenciesToScan>
...
</configuration>
...
</plugin>
...
</build>
You should also declare the actual dependencies in the dependencies section:
<dependencies>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>my-artifact</artifactId>
<type>test-jar</type>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>my-other-artifact</artifactId>
<type>test-jar</type>
<version>1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
No such luck. surefire wants to just run the tests that it can see in the sources.
This is currently not possible out of the box, surefire just looks at classes in target/test-classes:
Re: Surefire not picking up tests from test-jar
Re: maven-surefire-plugin: run unit tests from classes in a jar, not a directory
This is actually logged as SUREFIRE-569 - There should be a way to run unit tests from a dependency jar.
I can see a sadly obvious solution here involving the build-helper-plugin and adding the tests into the test compilation environment as a source directory, but I was hoping to avoid it.
The current workaround is to use dependency:unpack to unpack the jar into target/test-classes before the test phase.
Can't you do it the other way round?
I mean put the code the src/test/java, depend on your main module, and run the tests in your test module?