I am sorry if this is already answered, I tried to find a solution and still not getting anything.
I am working in Eclipse and Selenium webdriver and running the code via MAVEN.
My problem is that Maven continues running forever if an error was found, Selenium driver is not closed until I close the browser's window.
I can do it manually if anything happens in local, but the problem is when I try to use Jenknins, If anything fails, Jenkins job is running until I stop it.
Can you please help me?
Thanks in advance.
The important thing is to call WebDriver.quit() as you finish testing.
Usually in JUnit, this usually means calling it in one of these places:
a method or class annotated with #After or #AfterClass,
a property or class annotated with #Rule or #ClassRule,
a RunListener class, via the testFinished() or testRunFinished() methods,
a suitable runner class or
static teardown code.
You should choose the opposite to wherever you create your driver object. If all your tests are Selenium WebDriver ones and if you intend to run them sequentially in a single browser window (I think that this is the most common situation for smallish projects), then a single RunListener can hide this code nicely. As an example:
public class WebDriverContext extends RunListener {
public static WebDriver DRIVER; // Pretending to be final
public void testRunStarted(Description descr) {
DRIVER = new FirefoxDriver();
}
public void testRunFinished(Result result) {
DRIVER.quit();
}
}
(Credit to the OP for this answer, in his comment above. He's apparently gone from SO now, so he's not going to be able to post his answer here. I've also expanded it a fair bit.)
Related
I am trying to figure out how to work with test setup, body and teardown in the execution summary of an allure report generated from junit5. I am using aspectjweaver as jvm agent as this seams to be necessary to display the execution summary whatsoever.
I figured the junit5 #BeforeEach and #AfterEach methods should be displayed in test setup and teardown. Unfortunately for me they are always displayed inside the test body and setup/teardown do not show up at all.
In following example
#BeforeEach
public void before() {
before_step();
}
#Step("before step")
public void before_step() {
assertTrue(true);
}
the allure report will show the method in the test body like this.
Does someone have an idea why this is? Is this just not implemented correctly, do i need to implement something differently or do I maybe have a completely wrong idea as how this should be displayed?
Thanks for any help!
In order to enable test fixtures you need to use allure-junit5 dependency.
Then you can use AllureJunit5 extension on your tests - that will enable fixtures reporting.
The global configuration is also possible. To enable AllureJunit5 extension for all
your tests you need to start your JVM with -Djunit.jupiter.extensions.autodetection.enabled=true system property.
For more details on automatic extension registration please follow https://junit.org/junit5/docs/current/user-guide/#extensions-registration-automatic
I have a problem. There are 2 Test classes in my code and when I run each 1 test case manually both working fine. But when execute tests with maven only one test is executed successfully and other gives me error
java.lang.NullPointerException
at com.selenium.course.tests.ProductTests.executeProductTest(ProductTests.java:17).
Expected behavior: all tests should execute with maven.
Here is my code = https://github.com/Dermenji/SeleniumCourse
In your base calass where you have get methods for the driver, you need a "global" class variable which will be static.
public static WebDriver driver;
Selenium also works with annotations which they have not yet implemented.
For me, many test cases caused errors after I added annotations everything went well.
https://www.browserstack.com/guide/testng-annotations-in-selenium
maybe this Site helps u.
You should use same driver instance for all test classes
The solution is you can use util class for driver instance
I've managed to get my Android project transitioned over to JUnit4, and of course the main reason I wanted to do it isn't working. Would love any help if anyone's got ideas here.
The problem I'm trying to solve is that I want to automatically skip certain tests if the build is not pointed at the staging server. I've got this set up with a BUILD_TYPE which is using gradle to inject the base URL.
I set up an assumeThat clause in my setup which correctly identifies when the build is not staging, but instead of halting and ignoring the rest of the test, it throws an exception and fails.
Here's my base class for my live API tests - I've annotated descending from this with #RunWith(AndroidJUnit4.class), so in theory this should always be run with the JUnit4 runner:
package com.[my package].nonuitests.liveservertests;
import android.support.test.runner.AndroidJUnit4;
import com.[my package].nonuitests.BaseAndroidTest;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Tests against the live API. All tests descending from this class will
* be ignored if the BUILD_TYPE is not staging.
*/
#RunWith(AndroidJUnit4.class)
public class BaseLiveServerTests extends BaseAndroidTest {
private static final String STAGE = "staging";
/******************
* SETUP/TEARDOWN *
******************/
#Override
public void setUp() throws Exception {
super.setUp();
//TODO: Y U NO WORK?!
//This should cause the rest of the test to be skipped if it fails,
//but is instead throwing an AssumptionViolatedException.
assumeTrue(STAGE.equals(BuildConfig.BUILD_TYPE));
}
}
So, my questions:
Is there a better way to do this? In theory this could be done with flavors, but I was trying that earlier and it made everything else way more complicated.
My research indicates there's some kind of thing that Google's not implementing in their runner that's causing this to bomb out, but I'm having a hell of a time figuring out what I can/should do to fix this. Any suggestions of things I should subclass to get this to work as expected?
Any other thoughts would be most appreciated. Thanks!
Edit (1/26/15 11:40am CST): Per Grzesuav's suggestion, I took a stab at implementing this as an #Rule, but at the moment it's still not working. This seems a promising path, but it ain't working at the moment.
Edit 2 (1/26/15 12:15pm CST): OK, now it's working.
https://github.com/junit-team/junit/wiki/Assumptions-with-assume
ad 2) Custom runners could differently treat assume statement. To fix it you should write own version of Android runner and implement a way of dealing with assumes as native JUnit runner does or make a bug for android test runner.
ad 1) Suggested by me : try use JUnit Rules :
http://www.codeaffine.com/2013/11/18/a-junit-rule-to-conditionally-ignore-tests/
http://cwd.dhemery.com/2010/12/junit-rules/
OK, finally got it working with #Rules per Grzesuav's suggestion, although with significant changes since MethodRule has been deprecated. Here's a gist of what it turned out to be - I'll try to keep that updated as I refine it.
Some important notes:
You have to instantiate your #Rule in your test class, or you'll never actually hit any of your checks.
As of right now, this will not mark the test as ignored on Android, it'll just pass it without actually testing anything.
In Junit 4.12 it cannot handle tearDown
If you have tearDown you have to add an if statement with your condition rather than assumeTrue. I think the owners of Junit say it isn't supposed to work with #After
#After
override fun tearDown() {
if (junit == worksAgain()) {
Could you please look at my problem and give any advice to its solving.
I use JUnit4 and selenium 2 WebDriver.
So, I have class to run JUnit suite:
#RunWith(Suite.class)
#Suite.SuiteClasses({className1.class, clasName2.class})
public class TestSuite
{
public static TestSuite suite()
{
TestSuite suite = new TestSuite();
suite.addTest(new JUnit4TestAdapter(className1.class));
suite.addTest(new JUnit4TestAdapter(className2.class));
return suite;
}
}
each class contains #Test method and extends BaseClass that sets in #BeforeClass parameters (through DesiredCapabilities) to run suite on BrowserStack machines:
public class MyTestBase{
static protected WebDriver driver;
private boolean acceptNextAlert = true;
protected static StringBuffer verificationErrors = new StringBuffer();
#BeforeClass
public static void setUp() throws Exception {
DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setPlatform(Platform.WINDOWS);
capability.setCapability("build", "JUnit - Sample");
capability.setCapability("acceptSslCerts", "true");
capability.setCapability("browserstack.debug", "true");
driver = new RemoteWebDriver(
new URL("http://username:accesskey#hub.browserstack.com/wd/hub"),
capability);
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
}
/* other code */
}
So, could you please help me with the next:
1) I need to create configuration file and use its parameters to run my TestSuite on different browsers in BrowserStack. Any examples of .xml file to do it will be appreciated.
2) And also how do I need to modify my TestSuite.class to use .xml file parameters.
3) My TestSuite.class consists of many .class with #Test method in each. Each class extends MyTestBase.class where annotations #BeforeClass and #AfterClass are located, but when I run TestSuite new browser has been launched for each class in TestSuite and it's a very big problem for me. What can I do for running browser once for all #Test methods across all classes in TestSuite. I know that #BeforeClass works for all #Test methods inside one class, but what should be done if there are may classes? In TestNG there is #BeforeSuite solves this problem.
Sorry, for so many questions, but I've tried a lot and didn't succeed in this :(
Thanks a lot!
You asked a lot of questions concerning different topics. I will try to sort things out.
Reusing a browser and not opening a new one for every test class
This can only be achieved if you instantiate your WebDriver once and use that object in all your tests. So don't instantiate your browser in the #BeforeClass method of your tests.
How do you initialise your WebDriver?
In the very first test class of your test suite. That might be a dedicated test just for opening the browser. Or you could include this functionality in all of your test and have to check, if the browser had already been initialised or not.
How to reuse a WebDriver object and share it between test classes?
I'd propose to create a Singleton that stores a WebDriver object. This way all tests can access it. However, it takes some more effort to make this thread-safe - in case your run your tests in parallel.
Running your tests with different browsers
You should make your tests #Parameterized and expect a set of WebDriver objects as parameters to execute.
Combining both: Different browsers and reusing browsers between tests
This will likely lead to a point where you would like to define parameters for your test suite. However, in standard JUnit 4 you can't do this.
I recommend to use the ParameterizedSuite runner from this library.
I want to create custom html report for test run in JUnit. Problem I have is releasing resources and closing tags after all tests are done.
I keep one FileChannel opened for writing to report. Since it should be table with row for each test and there are hundreds of them, I don't want to open and close the channel for each test. Problem that appears here is tests organization - I have nested suites, so testRunFinished is not an option (refers to single suite, not all tests, and I saw this question). TestWatcher also will not help me, since it refers to single test only.
Tools used: maven 3.0.5, ff webdriver, junit 4.11.
I was considering two options:
1) opening and closing channel on each test run
2) overwriting finalize() to make it close the channel
None of them seems pretty... I've searched through many pages, but nobody seems to have the same problem I have.
Any prettier solutions?
Yes, see here (Before and After Suite execution hook in jUnit 4.x):
#RunWith(Suite.class)
#SuiteClasses({Test1.class, Test2.class})
public class TestSuite {
#BeforeClass
public static void setUp() {
System.out.println("setting up");
}
#AfterClass
public static void tearDown() {
System.out.println("tearing down");
}
}