Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Unit Test using JUnit (Java)

I just want to quickly ask, I found all over the internet and even here on SO, how Selenium IDE can create Java source files from what you are doing in browser. But all these sources result in some Unit Test. For Java I believe JUnit and some other are supported by Selenium IDE.

But I want to ask, why? I mean, if you still need to compile them before executing, why are Unit Tests used instead of just running the code and look if WebDriver throwns any exception? What is the advantage of using for example JUnit here? I know its mostly used this way, I just don´t know why. Thanks.

like image 662
B.Gen.Jack.O.Neill Avatar asked Jul 11 '11 12:07

B.Gen.Jack.O.Neill


People also ask

How do I run a JUnit test in Selenium?

For JUnit 4 To run a single test, execute a command via the command line: 'mvn -Dtest=DemoTest test'. To run all the tests in a batch, execute 'mvn test' in the command prompt.

Can we use JUnit for Selenium?

JUnit is an open-source framework for performing unit testing of Java-based web apps. You can also use it with Selenium WebDriver to automate the testing of your web applications. In addition, with JUnit, you can create test cases in a more structured and better format.

Is JUnit used for automation testing?

Besides manual testing, JUnit is preferred equally for automation testing. It can also be used along with the Selenium WebDriver to automate tests for web applications. It provides a unique way to write structured, short, and better test cases.

Can I use TestNG and JUnit together?

You can execute your existing JUnit test cases using TestNG. TestNG can automatically recognize and run JUnit tests, so that you can use TestNG as a runner for all your existing tests and write new tests using TestNG.


2 Answers

Here's a couple of reasons off the top of my head:

1) You can hook your selenium tests into your build process (and hence your CI process).

2) You can use JUnit assertions.

3) You can build up multiple suites of JUnit tests (which can then be run in parallel).

I'm sure there's more but I guess it depends on the number of tests you have and the size of the project you are working on. If you're project already has a set of JUnit tests then it's quite nice to be able to write selenium tests without too much effort.

like image 58
Mark Pope Avatar answered Oct 02 '22 02:10

Mark Pope


If you use Junit you can quickly recover from failures before starting a new test with the @before and @after annotations. You can also TearDown the tests with it. This also makes the tests more organized.

like image 26
Castilho Avatar answered Oct 02 '22 00:10

Castilho