I am starting to write WebDriver tests in Java having previously used C#/NUnit. I notice in any examples I have seen there is no Java version of the C# NUnit annotation for [TestFixture]. Is there such a thing in Java?
There is no equivalent annotation to [TestFixture] in JUnit. Classes with methods marked with #Test get run by most JUnit test runners without any other special markings; and #Before methods act like [SetUp]; and #After methods act like [TearDown].
Related
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 don't have the newest version of TestNG, and I have option to add #Test on constructor, java docs
#Target({METHOD, TYPE, CONSTRUCTOR})
public #interface Test {
I didn't find any clue on usage in docs or searching online
#Test Marks a class or a method as part of the test.
When/Why #Test can be define on constructor? is it for internal purposes only?
I saw that CONSTRUCTOR was removed in latest TestNG version, but I didn't find why.
See issue "Remove irrelevant "targets" for TestNG annotations": The target CONSTRUCTOR had no functionality in older versions, because constructors were not seen as test methods. That's why the target was removed later.
I'm using Selenide with Junit 5
Selenide 5.1.0
Junit 5.3.2
When I run tests with junit, same driver/browser is used for all tests. I want to create new WebDriver instance for each test.
I can call driver.quit() in AfterEach method. But I wanted to know is there any inbuilt method to handle the same in Selenide. So that I won't have to worry about driver initialization.
It should open a new driver for each Test and close after execution
It appears that there is no official JUnit Jupiter (JUnit 5) integration from Selenide:
https://github.com/selenide/selenide/issues/488
However, you could implement an Extension in JUnit Jupiter similar to the one available for Selenium:
https://bonigarcia.github.io/selenium-jupiter/
There are JUnit5 extensions in Selenide: https://github.com/selenide/selenide/tree/master/statics/src/main/java/com/codeborne/selenide/junit5
One of them, BrowserStrategyExtension, can reopen browser before every test.
You can implement Junit5 AfterEachCallback.
Here's an example:
public class CloseBrowserAfterEachTest implements AfterEachCallback {
#Override
public void afterEach(ExtensionContext extensionContext) throws Exception {
WebDriverRunner.closeWebDriver();
}
}
And then annotate your test classes (or your base test class) with #ExtendWith({CloseBrowserAfterEachTest.class})
I'm using WireMock in my tests and have such a line of code:
#Rule
public WireMockRule wireMockRule = new WireMockRule(8080);
I want to switch to JUnit 5. So I added the next dependency (using Gradle):
testCompile('org.junit.jupiter:junit-jupiter-engine:5.1.1')
But there are no suggestions when I'm trying to import #Rule annotation.
Do I need to add another module of JUnit dependency? Or are rules not supported in JUnit 5? If not, how can I replace #Rule annotation to make tests work again?
In a general way, what you did with #Rule and #ClassRule in JUnit 4 should be done with #ExtendWith and Extension that associated provide a very close feature in JUnit 5.
It works as standards JUnit lifecycle hooks but that it is extracted in a Extension class. And similarly to #Rule, as many Extensions as required may be added for a test class.
To handle the issue you have several possible approaches among :
keep the JUnit 4 way (JUnit 5 owns the JUnit Vintage part that allows to execute JUnit 3 or 4 tests).
rewrite the #Rule as an Extension.
do the actual processing done by WireMockRule (start the server, execute your tests and stop the server) in each test of class with #BeforeEach and #AfterEach hook methods.
use a third library that implements the equivalent of WireMockRule in the JUnit 5 Extension way such as https://github.com/lanwen/wiremock-junit5
Note that your issue already discussed in the JUnit 5 Issues.
JUnit 4 annotations #Rule and #ClassRule do not exist in JUnit 5. Basically there is a new extension model that can be used to implement extensions with the same functionality. These extensions can be used with the #ExtendWith annotation.
There is a limited migration support for a subset of JUnit 4 rules in the junit-jupiter-migrationsupport module. Unfortunately, it's only limited to subclasses of ExternalResource and Verifier.
Before wiremock has official support for JUnit you have some workarounds:
Run JUnit 4 tests side by side with JUnit 5 tests with the junit-vintage-engine.
Start and stop the server yourself in the test code.
Use a 3rd party extension like wiremock-junit5 or wiremock-extension.
There is now official support for JUnit 5 Jupiter from WireMock 2.31.0.
Docs here: http://wiremock.org/docs/junit-jupiter/
The https://github.com/webcompere/java-test-gadgets project lets you solve this in a couple of ways.
You can use its support for JUnit 4 rules via the DangerousRuleAdapter - which will attempt to turn any JUnit 4 rule into a Plugin:
#ExtendWith(PluginExtension.class)
public class DangerousRuleAdapterExampleTest {
#Plugin
private DangerousRuleAdapter<WireMockRule> adapter =
new DangerousRuleAdapter<>(new WireMockRule());
#Test
void theTest() {
// use wiremock rule here
WireMockRule rule = adapter.get();
}
The rule adapters cannot work with rules that inspect the test class or the test method, but make a good attempt at running the rule.
There's also support for running a rule around some code:
TemporaryFolder temporaryFolder = new TemporaryFolder();
// let's use this temp folder with some test code
executeWithRule(temporaryFolder, () -> {
// here, the rule is _active_
callSomethingThatUses(temporaryFolder.getRoot());
});
And you can easily create your own new JUnit 5 plugin by using the PluginExtension and TestResource.of
#ExtendWith(PluginExtension.class)
class TestResourceIsActiveDuringTest {
private WireMockServer server;
#Plugin
private TestResource someResource = TestResource.from(() -> server.start(),
() -> server.stop());
From the JUnit 5 user guide:
#Rule and #ClassRule no longer exist; superseded by #ExtendWith and #RegisterExtension. See also "Limited JUnit 4 Rule Support".
However, as pointed out by Tom, WireMock has full JUnit Jupiter support since version 2.31.0:
// New JUnit 5 extension
#WireMockTest
class DeclarativeWireMockTest {
#Test
void test_something_with_wiremock(WireMockRuntimeInfo wmRuntimeInfo) {
// The static DSL will be automatically configured for you
stubFor(get("/static-dsl").willReturn(ok()));
// Instance DSL can be obtained from the runtime info parameter
WireMock wireMock = wmRuntimeInfo.getWireMock();
wireMock.register(get("/instance-dsl").willReturn(ok()));
// Info such as port numbers is also available
int port = wmRuntimeInfo.getHttpPort();
// Do some testing...
}
}
For more information, please refer to the corresponding docs.
I have upgraded from testng 6.3.1 to 6.14.2 and after this I am getting error on org.testng.annotations.Configuration as the class does not exist.
I just want to know how to replace this?.I have checked the deprecated API and found that this is deprecated in 6.9.x.
Thanks
TLDR; Use the dedicated annotations instead, eg: #BeforeTest instead of #Configuration(beforeTest = true).
TBH I never used it, I've always used the dedicated #BeforeTest, #AfterTest, etc annotations, but out of curiosity I've done a bit of research:
according to the change log (currently line 1129) it's been deprecated since v 5.0 of 2009/04/01: Added: Deprecated #Configuration and introduced #BeforeSuite/Test/Class/TestMethod
it's been removed with this commit
before its removal (up to v6.11) it's javadoc read:
Configuration information for a TestNG class.
Deprecated.
Use #BeforeSuite, #AfterSuite, #BeforeTest, #AfterTest, #BeforeGroups, #AfterGroups, #BeforeClass, #AfterClass, #BeforeMethod, #AfterMethod
usage samples:
#Configuration(beforeTest = true)
public void setUp() {
...
In conclusion, just use the newer individual annotations instead.