I have extended Selenium using the Java WebDriver library and the TestNG framework. When running test scripts, I notice an inordinate amount of time for the test to start execution, when the test takes in input parameters from an Excel file (using the #DataProvider annotation).
The delay can amount to about 10 min, which makes it time consuming to run and debug. Is there a reason for this significant delay?
Yes could be because of the way you are reading from excel (greedy data provider) and depends on how big your excel file is. There is something called lazy data provider. Found an example of one here . Posting the code from the link.
For better understanding need to see your code.
public class LazyDataProviderExample {
#Test(dataProvider = "data-source")
public void myTestMethod(String info) {
Reporter.log("Data provided was :" + info, true);
}
#DataProvider(name = "data-source")
public Iterator<Object[]> dataOneByOne() {
return new MyData();
}
private static class MyData implements Iterator<Object[]> {
private String[] data = new String[] { "Java", "TestNG", "JUnit" };
private int index = 0;
#Override
public boolean hasNext() {
return (index <= (data.length - 1));
}
#Override
public Object[] next() {
return new Object[] { data[index++] };
}
#Override
public void remove() {
throw new UnsupportedOperationException("Removal of items is not supported");
}
}
}
For some reason, this issue was resolved by rebuilding my custom Firefox profile - it may have gotten corrupt.
Just posting this as an answer for reference, in case any one is bogged down by this issue.
Related
I just complete a Java CONSOLE application for Student Management.
I received a test case set (pdf file contains lines follow according to the requirements of the application) build based on the standard program (from my lecturer). You can overview what my app do and what is format of test casenter image description heree set in the attached image below.
The problem is that I want to use test cases for testing my app but instead of manually entering and matching line by line between Console IO and the pdf file => I want to write a program to automatically import and match the data between my jar/program to test cases.
However, I'm not sure how and where to start.
I have tried with google but unit test/white testing is still the thing that takes up all of my search. Hopefully in the process of continuing to try to search with google, someone will give me some suggestions or directions that will be useful to me. Thanks very much.
[My Program]
[Test cases set]
The way I'd do it is to decouple your application from the console so that you can use fake implementations for printing and reading from the console in your tests. "Fake" is the technical term - you can look up "test doubles" to learn about those and other related ideas. This idea is known as dependency injection, or the dependency inversion principle.
The way we do this is to use interfaces. Here's an example of an application that prints some items:
import java.util.List;
public class ItemPrinterApplication {
public ItemPrinterApplication(OutputWriter outputWriter, List<Item> items) {
this.outputWriter = outputWriter;
this.items = items;
}
public void run() {
outputWriter.writeLine("Name, Price");
items.forEach(item -> outputWriter.writeLine(item.name + ", " + item.price));
}
private OutputWriter outputWriter;
private List<Item> items;
}
OutputWriter is the thing responsible for the printing. It's just an interface, so the application doesn't know whether it writes to the console or somewhere else:
public interface OutputWriter {
void writeLine(String line);
}
For completeness, the Item class just holds some data:
public class Item {
public Item(String name, Integer price) {
this.name = name;
this.price = price;
}
public final String name;
public final Integer price;
}
I can then write a test using JUnit that checks that when I run this application, I get the output that I want. I do that by using an implementation of OutputWriter that just writes to a string. That way it's easy to check in the test:
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
public class ItemPrinterTest {
#Test
public void itPrintsAListOfItems() {
List<Item> items =
List.of(
new Item("Apple", 50),
new Item("Carrot", 25),
new Item("Milk", 120)
);
FakeOutputWriter fakeOutputWriter = new FakeOutputWriter();
ItemPrinterApplication app = new ItemPrinterApplication(fakeOutputWriter, items);
app.run();
Assertions.assertEquals(
"Name, Price\n" +
"Apple, 50\n" +
"Carrot, 25\n" +
"Milk, 120\n",
fakeOutputWriter.written
);
}
}
and FakeOutputWriter looks like
public class FakeOutputWriter implements OutputWriter {
public String written = "";
#Override
public void writeLine(String line) {
written += line;
written += "\n";
}
}
This gives me confidence that I'm writing the output correctly. In main, though, I want to actually print to the console:
import java.util.List;
public class Main {
public static void main(String[] args) {
OutputWriter outputWriter = new ConsoleOutputWriter();
List<Item> items =
List.of(
new Item("Apple", 50),
new Item("Carrot", 25),
new Item("Milk", 120)
);
new ItemPrinterApplication(outputWriter, items).run();
}
}
and ConsoleOutputWriter does exactly that:
public class ConsoleOutputWriter implements OutputWriter{
#Override
public void writeLine(String line) {
System.out.println(line);
}
}
You could take the same approach for faking reading input. Your interface would have a function that takes no arguments and reads a string:
interface InputReader {
String readLine()
}
so in the tests you could fake that and in main, read using a Scanner or something.
i recently started playing around with tdd and ran into a problem where i do not understand why one thing is working and the other one doesnt.
the following code works for me:
public class Ant {
public Ant(Point startLocation, Point hive) {
this.currentLocation = new Point(startLocation);
this.hive = new Point(hive);
}
public void goHome() {
if (hive.x > currentLocation.x) {
currentLocation.x++;
} else if (hive.x < currentLocation.x){
currentLocation.x--;
}
if (hive.y > currentLocation.y) {
currentLocation.y++;
} else if (hive.y < currentLocation.y){
currentLocation.y--;
}
}
}
The corresponding test:
#DataProvider(name = "goneHome")
public static Object[][] goHome() {
return new Object[][] {
{new Point(2,1), new Point(3,2), new Point(7,8)},
{new Point(20,1), new Point(19,2), new Point(7,8)},
{new Point(23,10), new Point(22,9), new Point(7,8)},
{new Point(2,10), new Point(3,9), new Point(7,8)},
{new Point(2,8), new Point(3,8), new Point(7,8)},
{new Point(7,1), new Point(7,2), new Point(7,8)}
};
}
#Test(dataProvider = "goneHome")
public void testGoHome(Point currentPosition, Point nextPosition, Point hive)
throws Exception {
Ant ant = new Ant(currentPosition, hive);
ant.move();
assertEquals(ant.getCurrentLocation(), nextPosition);
}
the test fails if i change the ant constructor like this:
public Ant(Point startLocation, Point hive) {
this.currentLocation = startLocation;
this.hive = hive;
}
By failing i mean that the test with the first two sets of the DataProvider work correctly, the rest is failing/not finishing.
Although i am not quite sure what failed. If i remove the first two sets of data in the DataProvider, still only the first two datasets (which where the 3rd and 4th data set before) do not fail.
I use IntelliJ and the symbol besides the "failed" test is still the "loading icon".
Debugging each single test case shows that the points are set correctly. Removing the assert from the test does not change anything.
Can someone explain this behavior to me please?
Thanks in advance
Egon
Edit: corrected the version of the constructor that failed
Maybe it's a bug in IntelliJ IDEA. Sometimes I also facing with this problem. Unfortunatelly it's still (2014-11-24) unresolved: https://youtrack.jetbrains.com/issue/IDEA-100752
Try run your tests with alternate runner (as Maven goal, for instance).
This is a sample program:
public class FunctionalTest {
public int f(int r) {
int result = r * 5;
return result;
}
public static void main(String[] args) {
FunctionalTest funct = new FunctionalTest();
System.out.println(funct.f(5));
}
}
I'm a beginner.
How to write a functional test for this code?
How to write functional tests? Do I need to TestNG?
Is it enough to write the examination method?
Could someone explain to me and write a sample functional test for this program?
Well, if you're specifically asking for functional testing, there's not much you can do with that code snippet. You can do a unit test from the f method using JUnit like this:
#Test
public void testF(){
FunctionalTest t1 = new FunctionalTest();
assertEquals((t1.f(1) % 5), 0); //checks that is getting multiplied by 5.
}
However, you want functional testing, so by running your compiled app and assessing the result you're testing your app functionality by multiple units (AKA integration): your f method and your main method.
Regards!
First of all, you need to have a clear definition of contract you want to verify. From the code, I assume it is something like "the method should return the number equal to argument multiplied by 5".
TestNG, JUnit or other test frameworks is not mandatory for your case. The test may look like:
public void testF() {
int arg = 5;
int result = new FunctionalTest().f(arg);
assert result == arg * 5;
}
Also please keep in mind that to use assert you need JVM started with -ea flag.
Beware the terms you used:
the functional testing provide values to your user/customer
That implies:
black box testing: you have to test your whole system (hard+soft)
the test should target your user/customer needs (explicit report or test)
You can use whatever you want to test the feature (from unit test to jbehave).
In your case (using JUnit 4 and AssertJ):
import org.assertj.core.api.Assertions;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
/*
As an user
I want have 25 whatever I sent
*/
public class NumberGenerationTest {
private static final String PATH = "directory of your class file";
private InputStream stdout;
/* Nominal case */
#Test
public void shall_return_number_25_when_called_with_5() throws Exception {
when_I_call_FunctionalTest_with("5");
then_it_returns("25");
}
/* Nominal case or potential error case */
#Test
public void shall_return_number_25_when_called_with_10() throws Exception {
when_I_call_FunctionalTest_with("10");
then_it_returns("25");
}
/* Nominal case or potential error case */
#Test
public void shall_return_number_25_when_called_with_ABC() throws Exception {
when_I_call_FunctionalTest_with("ABC");
then_it_returns("25");
}
private void when_I_call_FunctionalTest_with(String parameter) throws Exception {
ProcessBuilder builder = new ProcessBuilder("java" ,"-classpath", PATH,"FunctionalTest" , parameter);
builder.redirectErrorStream(true);
Process process = builder.start();
stdout = process.getInputStream ();
}
private void then_it_returns(String expectedResult) throws Exception {
BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
String line = reader.readLine ();
Assertions.assertThat(line).isNotNull();
Assertions.assertThat(line).isEqualTo(expectedResult);
}
}
It seems you have an error in your main() ... or not.
Request:
This is a very common problem faced by Java devs in my locale. I am really stuck for many days on this. Searched and tried a lot, read the docs. read ALL the stackoverflow questions related to JavaExe. Please only reply if you have done similar thing before and have a comprehensive answer. I would be really grateful to the community!
Senario:
I am using JavaExe to run an application as system service in desktop interactive capability.
To be exact I have an application that captures screenshots of desktops. I want it to run (as admin) on any user login so no one can stop it.
I have a myapp.jar, settings.txt and a lib dir.
I have searched alot and found JavaExe works (by watching its examples)
If anyone has a better way. Please state so.
Problem:
According to my research,
you must create a .properties file that has named like the .exe, and write "RunType = 1" in this file.
you define a static method in your main class : serviceInit()
Do I need to place any class or reference/import? How?
Edit:
My code below works as stand alone .jar and in javaExe.exe too.
It now does makes a system service and runs by as SYSTEM user. but It is NOT interactive to desktop. i.e its not showing any GUI.
package temp;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class Temp {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
serviceInit();
}
public static boolean serviceInit(){
new Thread(){
public void run(){
Integer i = 0;
while(i < 999999999){
JOptionPane.showMessageDialog(null,i);
i++;
}
}
}.start();
return true;
}
}
And I dont think that bundling the .jar, lib directory and settings.txt into one .exe is possible?
you should have in your case :
public class MyApp_ServiceManagement
{
static boolean isMsgToDisplay = false;
/////////////////////////////
public static boolean serviceInit()
{
(new Thread()
{
public void run()
{
for(int i=0;i < 6;i++)
{
try { sleep(5*1000); }
catch(Exception ex) {}
isMsgToDisplay = true;
}
}
}).start();
return true;
}
/// is Data ready to be send to the UI ?
public static boolean serviceIsDataForUI()
{
return isMsgToDisplay;
}
/// Data to be send to the UI
public static Serializable serviceDataForUI()
{
isMsgToDisplay = false;
return "hello, I am an interactive Service";
}
}
and for the UI part :
public class MyApp_TaskbarManagement
{
/// To show (or not) the icon in tray
public static boolean taskIsShow()
{
return false;
}
/// Receive the message from Service
public static void taskDataFromService(Serializable data)
{
JOptionPane.showMessageDialog(null, data);
}
/// descr of UI
public static String[] taskGetInfo()
{
return new String[]
{
"UI part of Service"
};
}
}
the main() method is never called in service mode (except one particular case), but if you want keep your main() method you must put a call to main() in serviceInit().
Put serviceInit() in your main class or in another class named XXX_ServiceManagement where XXX is the name of your main class.
Then, serviceInit() must return before a 30 seconds delay.
Don't put a infinite loop, ... in it.
Put your code in a thread, and start it from serviceInit() (or main)
That answer to your problem?
So here's my dilemma. I have been using Selenium, TestNG, and iText to generate nice PDF reports from the results of an automated Test run, but I was told recently that they didn't want competing libraries TestNG vs. JUnit, and was told to start using JUnit instead.
I am running these JUnit tests with parameters, and am wondering is there a way to access the parameter after/during the test run? The parameters are Strings with browser names which is used to tell selenium which WebDriver to get, and it would be nice to know that a test passed/failed in a certain browser. JUnit seems to be very limited in the information you can access once a test run completes.
I have a class which extends junit.framework.TestListener which listens for the start/stop of each test, and here is where I can gather information about the test.
currentTest is of type BaseTestResult which is a class I wrote that simply stores test results in a list.
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestListener;
import junit.framework.TestResult;
import utilities.reporting.BaseReporting;
import utilities.reporting.BaseTestResult;
import utilities.reporting.ResultsPerSuite;
public class BaseListener implements TestListener {
private ResultsPerSuite resultsPerSuite;
private BaseReporting baseReporter;
private BaseTestResult currentSuite;
private BaseTestResult currentTest;
private long startTime;
private long endTime;
private long suiteStartTime;
private long suiteEndTime;
public BaseListener() {
baseReporter = new BaseReporting();
resultsPerSuite = new ResultsPerSuite();
currentTest = new BaseTestResult(null, null);
}
public void startSuite(Test suite) {
suiteStartTime = System.currentTimeMillis();
currentSuite = new BaseTestResult(suite);
}
#Override
public void startTest(Test arg0) {
startTime = System.currentTimeMillis();
currentTest = new BaseTestResult(arg0);
}
#Override
public void addError(Test arg0, Throwable arg1) {
currentTest.addError(new BaseTestResult(arg0, arg1));
}
#Override
public void addFailure(Test arg0, AssertionFailedError arg1) {
currentTest.addFailed(new BaseTestResult(arg0, arg1));
}
#Override
public void endTest(Test arg0) {
endTime = System.currentTimeMillis();
currentTest.setRuntime(startTime - endTime);
// If both empty, then test passed, so add to passed results.
if (currentTest.getFailed().isEmpty()
&& currentTest.getErrors().isEmpty()) {
resultsPerSuite.addPassed(currentTest);
} else {
resultsPerSuite.addFailed(currentTest);
}
}
public void endSuite(TestResult testEventDriver) {
suiteEndTime = System.currentTimeMillis();
currentSuite.setRuntime(suiteEndTime - suiteStartTime);
resultsPerSuite.setSuite(currentSuite);
baseReporter.printToConsole(resultsPerSuite);
}
/**
* #return the allTestResults
*/
public ResultsPerSuite getAllTestResults() {
return resultsPerSuite;
}
}
I am not sure how to get it to do what you are looking for trivially from inside of the TestListener without creating your own Runner. But you might be able to handle it with a TestRule.
Since you appear to be using the listener to send the results to an external service, you might have better luck specifically by rigging in a TestWatcher that communicates with your own listener. It would have access to the local class member variables and could report on them fairly easily.