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.
Related
I have been working on TestNG and there was a problem associated with some of my code. Here's the code:
public class Main {
public String baseurl ;
public WebDriver webdriver ;
protected Main(){
//baseurl = "http://goodreads.com";
//webdriver = new FirefoxDriver();
}
#BeforeSuite
public void setup(){
baseurl = "http://goodreads.com";
webdriver = new FirefoxDriver();
}
Above code is of the base class
#Test
public void do_login(){
super.webdriver.get(super.baseurl);
This is of inherited class
Now as far as I know the BeforeSuite should executed first and the values of baseurl and webdriver should get initialized to the specified values. But I'm getting a NullPointerException in the above code. The problem is resolved when I assign the variables as static or if I initialize the variables in the constructor. But why are they not being initialized when I put them in the method(which is supposed to execute before the #Test executes anyway)? Is there some concept about java that I'm missing? Please explain
You don't need to include both classes to the suite. Only #Test containing class should be included. I think that's the problem. I also believe that #BeforeSuite annotated method will be executed not depending on the way you run your tests (as TestNG method or TestNG suite), since default suite will be created in order to run tests anyway.
I am having a problem, one test class seems to be interfering with the other in my test suite.
I have a suite, which executes two classes, one is called MergeTestSuite.java (which is another Suite), and the other is called RecordTest.java.
RecordTest extends one class already tested by MergeTestSuite.java
I created another suite as follows:
#RunWith(Suite.class)
#Suite.SuiteClasses( {
MergeTestSuite.class,
RecordTest.class
})
public class CoreTestSuite {
#BeforeClass
public static void install() throws Throwable {
RegistryUtils.cleanupResources();
}
}
Both MergeTestSuite.class and RecordTest.class run fine individually. If I run CoreTestSuite, the second test will fail, unless I remove MergeTestSuite.class from the list.
A Junit TestSuite provides extra features for multiple tests. For instance, you can control the order in which the tests run and you can also combine multiple test suites into another suite. I think this older doc from Junit 3.1.8 describes it best.
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");
}
}
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.)
What is the best way run a lot of integration tests using JUnit?
I crudely discovered that the code below can run all the tests... but it has a massive flaw. The tearDown() method in each of those classes is not called until they have all been run.
public class RunIntegrationTests extends TestSuite {
public RunIntegrationTests(){
}
public static void main (String[] args){
TestRunner.run(testSuite());
}
public static Test testSuite(){
TestSuite result = new TestSuite();
result.addTest(new TestSuite(AgreementIntegrationTest.class));
result.addTest(new TestSuite(InterestedPartyIntegrationTest.class));
result.addTest(new TestSuite(WorkIntegrationTest.class));
// further tests omitted for readability
return result;
}
}
The classes being run connect to the database, load an object, and display it in a JFrame. I overrode the setVisible method to enable testing. On our build machine, the java vm runs out of memory when running the code above as the objects it has to load from the database are pretty large. If the tearDown() method was called after each class finished it would solve the memory problems.
Is there a better way to run them? I'm having to use JUnit 3.8.2 by the way - we're still on Java 1.4 :(
Not sure if this is the problem, but according to the JUnit Primer you should just add the tests directly, instead of using the TestSuite:
result.addTest(new AgreementIntegrationTest()));
result.addTest(new InterestedPartyIntegrationTest()));
result.addTest(new WorkIntegrationTest()));
That's very strange. setUp and tearDown should bookend the running of each test method, regardless of how the methods are bundled up into suites.
I typically do it slightly differently.
TestSuite suite = new TestSuite( "Suite for ..." ) ;
suite.addTestSuite( JUnit_A.class ) ;
suite.addTestSuite( JUnit_B.class ) ;
And I just verified that tearDown was indeed being called the correct number of times. But your method should work just as well.
Are you sure tearDown is properly specified -- e.g. it's not "teardown"? When you run one test class on its own, is tearDown properly called?