I have developed UI page which has href to external link. Now this external link is built (with db based key values for http request parameters) in a class which extends GenericLink of Tapestry. I am overriding public ILink getLink(IRequestCycle cycle) and public String getHref() methods for that. The developed task is working as expected.
Now I want to write JUnit tests for this class, but I read there is other mechanism for Unit testing in Tapestry but this is again not clear to me w.r.t. GenericLinks.
Any leads to any artical will really help in order to test GenericLink class.
Related
I have Karate scenario defined as below
Feature: Random Users
Background:
* url 'https://askuser.me'
#get-user
Scenario: Get Random User data
Given path 'api'
When method get
Then status 200
* string json = response
* def Util = Java.type('com.example.mobiletest.utils.TestUtils')
* def SaveResponse = Util.writeToJSONFile(json,'randomuser.json')
And is Corresponding Runner class defined as below:
public class RandomUserRunner {
#Karate.Test
public Karate testRandomUserRunner(){
return Karate.run("RandomUser").relativeTo(getClass());
}
}
I want to execute testRandomUSerRunner() programatically from other java function, how do I do that (reason this is, karate scenario fetches response and saves in json file, other method in java want to reuses these steps)
I tried to call as below but it didnt work:
RandomUserRunner runner = new RandomUserRunner();
runner.testRandomUserRunner();
Anyhelp or pointers would be really appreciated.
First - a disclaimer. Karate is not designed for this. It looks like you are already using some Java utils from Karate, so I personally think trying to call Karate from Java is wrong. The JUnit classes exist to take care of reporting, and here also - the parallel Runner is recommended: https://stackoverflow.com/a/65578167/143475
That said, see if the Runner.runFeature() API meets your use case. You will be able to access variables created by the Feature also.
Refer: https://github.com/karatelabs/karate#invoking-feature-files-using-the-java-api
I am reading a code of GWT
Basically in this project they are getting some constant value like button text from a properties file.
so they have an interface LocalizableResource and getting the instance like
public interface LocalizableResource extends Constants {
public static class Util {
public static LocalizableResource getInstance() {
return GWT.create(LocalizableResource.class);
}
}
String lblName_text_1();
}
and use this instance to get a button text
String buttonText = LocalizableResource.Util.getInstance().lblName_text_1();
Button b = new Button(buttonText);
in java we can not Instantiates an interface then,
How GWT doing this such like. I have not so much Idea about deferred binding and GWT.
That's the beauty of GWT and one of its way to manage multiple clients which is the core advantages of GWT framework.
http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsDeferred.html
Deferred binding is a feature of the GWT compiler that works by generating many versions of code at compile time, only one of which needs to be loaded by a particular client during bootstrapping at runtime. Each version is generated on a per browser basis, along with any other axis that your application defines or uses. For example, if you were to internationalize your application using GWT’s Internationalization module, the GWT compiler would generate various versions of your application per browser environment, such as “Firefox in English”, “Firefox in French”, “Internet Explorer in English”, etc… As a result, the deployed JavaScript code is compact and quicker to download than hand coded JavaScript, containing only the code and resources it needs for a particular browser environment.
A tag interface that facilitates locale-sensitive, compile-time
binding of constant values supplied from properties files. Using
GWT.create(class) to "instantiate" an interface that extends Constants
returns an instance of an automatically generated subclass that is
implemented using values from a property file selected based on
locale. more info
I am unit testing my application with JUnit. It uses (or tries to use...) the MVP pattern, hibernate and a swing gui.
In my test, I am testing whether persistence works as expected, e.g. is the date format correct, are the relationships between the database tables correct, etc.
Here's an (edited) example:
#Test
public void testCheckBeitragCalculation() {
MainView view = new MainView();
view.initGUI();
Controller persist = new Controller(view);
persist .saveData(testObject);
Controller edit = new Controller(view);
Controller search = new Controller(edit);
search.getData("10001"); // primary key
TestObject t2 = edit.getViewData();
assertEquals(10, t2.getBeitrag());
}
I initiate the main view because two controllers needs a reference to the main view to pass data.
Controller persist persists the object, Controller edit is the controller for the editing view and Controller search needs a reference to edit because it looks up an entity in the database and then displays this data in the editing view through the edit controller.
Because these controllers display the data in the GUI, the GUI shows up a split second during the unit test.
Is that bad practice? I've read that you shouldn't test GUI code, but I'm not testing the GUI itself, it just gets called by the tested code.
Thanks for advice!
There is no right answer to this. In general if it solves your purpose then you can go ahead with this but again if in future your product build system adds these junit test cases to execute after each build through scheduled daily build (e.g. jenkins) and the box where build runs (generally some server) doesn't support any UI (or it is not available at that particular time) like XWindows then your test might fail.
I have written my even first code using Selenium Web driver, Maven and Java. Can someone give expert opinion if I am in the right direct and if this particular code can be improved.
public class myFirstTest {
#Test
public void startWebDriver(){
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.ft.com/home/uk");
Assert.assertTrue("title should start with World Business", driver.getTitle().equals("World business, finance, and political news from the Financial Times - FT.com"));
driver.findElement(By.id("ftLogin-signIn")).click();
driver.findElement(By.xpath("/html/body/div[4]/div[2]/div[1]/div[2]/div[2]/div[3]/form/fieldset[1]/input")).sendKeys("xxx#mail.com");
driver.findElement(By.id("ftLogin-password")).sendKeys("xxxxxxxx");
driver.findElement(By.xpath("/html/body/div[4]/div[2]/div[1]/div[2]/div[2]/div[3]/form/fieldset[3]/p[1]/button")).submit();
driver.quit();
}
}
Depending is you are learning or creating a test framework for your company there are some pointers.
You functions and values for finding elements are not optimal. cssSelector or id is to prefer.
;
By.cssSelector("input[id$='ftLogin-username']");
or
By.id("ftLogin-username");
Never use xpath if not is absolutly necessary. Escpecially those dependending on the DOM object. Instead change the webpage implemenation to set an id on the webpage elements.
For the button which doesnt have a unique class or id i would use the text instead
driver.findElement(By.cssSelector("button:(*'Sign in'*)"));
If this login part is to be used in several test cases, create a helper class with a login function instead. Otherwise you have to wright the same code over and over again. If a id or xpath where to change you also have to update all your test cases which uses that element.
Add an assertion att the end of the test case to verify that the login has been successful.
Definitely the code you have written can be improved.
Considering the fact, you have just started with WebDriver, below are few suggestions-
Avoid using such long xpaths for locating elements. It has lots of
dependency on DOM. Use short xpaths wherever possible or go for
using other locators.
You can read data from files(s) required for your tests.(example : username, password)
You can use annotations such as #BeforeClass, #BeforeMethod etc., if you are using any unit testing framework to run your tests such as JUnit, TestNG.
I am trying to encode input form data here. There are two options and I have tried both of them:
Use URLencoder.encode(inputString) method which does not work on GWT client side (My code resides in client module) Results in error 'Did you forget to inherit required module?'
URL.encodeQueryString(inputString) which works well, But when I run relevant test cases using JUnit, all I get is unsatisfiedlinkederror
Are there any alternatives for encoding method or is there any work around for above mentioned methods?
For your second option :
GWT uses modules and needs to be compiled, which is different than running a simple JUnit test. Take a look at http://www.gwtproject.org/doc/latest/DevGuideTesting.html, they explain how to setup JUnit test.
Just use the URL class and its methods:
URL.encode(String decodedURL)
URL.encodeQueryString(String decodedURLComponent)
Do not forget to inherit the required module <inherits name="com.google.gwt.http.HTTP"/>.
For URL building, I use the "UrlBuilder": com.google.gwt.http.client.UrlBuilder
UrlBuilder u = new UrlBuilder();
u.setProtocol("https");
u.setHost("www.mysite.com:8080");
u.setPath("/myServletPath");
u.setParameter("username", nameField.getValue());
u.setParameter("someAttribute", "itsValue");
u.buildString();
This code will result in:
https://www.mysite.com:8080/myServlet?username=GWT%20User&someAttribute=itsValue