Test for checking object values inside class - java

I have a javafx application which has a few fields, like an anchorPane. In its .fxml file, that anchorPane has a few fields like these
How can I test these values? I'm sure that JUnit can test object values, so I was wondering if I can make something like
Assertions.assertThat(mainAnchorPane.getId()).isEqualTo("mainAnchorPane");
Not sure but that might be as simple as instantiating an entity class (lets name it Person) and do something like
Person person = new Person("Maria");
Assertions.assertThat(person.getName()).isEqualTo("Maria");
The point here is that I'm not sure how can I inject the object values from my class into the tests, assuring that Person (or AnchorPane) from my class has the same values in my test.
Any thoughts?
Thanks in advance!

I don't know if you already thought about this, but there is a test library called TestFX. It is made to test JavaFX applications easily. Maybe it can help you:
https://github.com/TestFX/TestFX/
For example you can proof the input of textfields or simulate mouse clicks. You can find more information on the given url.

Related

Selenium Java - Page Object Model Query

Imagine there is a page say http://google.com/AddUser and here you enter details for a record and click save. Once you do this the page redirects to
http://google.com/userList
where you can see list of users including the new record you just entered.
If we are going by page object model, the method to enter details and save record should exist on AddUser.java and the method to validate if the record was actually saved and displayed should be on UserList.java
If we consider addUser and userList are the corresponding objects for both classes it will be something like below :
addUser.enterDetailsSaveRecord();
userList.validateSavedRecord();
So in my Test case i would need to call 2 separate methods, one for the action and other to validate.
Both AddUser.java and UserList.java have BasePage.java as the superclass.
Is there a way to club them both into a single method or is there something I'm going about in a wrong way and is there a better approach?
Thank you
Using PageFactory you are having 2 PageObjects as AddUser.java and UserList.java. So assuming you are passing the appropriate arguments while invoking the methods, the following works for you:
addUser.enterDetailsSaveRecord();
userList.validateSavedRecord();
But a word from Best Practices, Assertions should be done in a seperate utility/package/class which is in similar line with #JeffC comment:
Best practice is to keep the validation code out of the page objects
Hence, you should be creating a seperate common utility/package/class which will handle all the Assertions. You can call the class containing the Assertions from your PageObject class as well.
So your entire Test Environment will contain 3 Packages. One package containing the main()/#Test class, one package containing the PageObjects e.g. AddUser.java and one Utility package with the class for containing the Assertions e.g validateSavedRecord().
I don't see anything wrong with your approach either, although, my approach is usually to logically separate functional interaction with the application from testing functions. So, I would still have
addUser.enterDetailsSaveRecord();
but for userList I would use
UserItem foundUser = userList.findUser(targetUser);
where UserItem is a row in the table of users. My test would then verify that foundUser was correct.
Although this ends up with a few more lines of code, it results in the object model cleanly and simply modeling the object under test, and the testing code being found in the test itself.
Your approach is correct. These methods should belong to different pages.
Please update method as:
public UserList enterDetailsSaveRecord() {
// your code to save the details
return new UserList();
}
thus you can use it as:
addUser.enterDetailsSaveRecord().validateSavedRecord()

Selenium test organisation of classes

Ok selenium gurus a bit of an open question here. I am looking for some guidance on the best way to organize my tests that use object oriented principles.
At the moment I am creating a testrunner main class from which I create an object of a general test class. I am then extending this class for more granular tests.
An example.
I need to open the browser, enter the url, log in as a user.
From there you can access perhaps 40 different links each containing their own pieces of functionality. E.g. A profile link which leads to a profiule screen where you can enter introduction text, upload a picture, change a picture etc...
Another example would be a notification screen where you can navigate to view and mark as read etc...notifications you have received.
I can write the code to test this by for example by creating a ton of methods in that 1 class and then calling these from the main testrunner class. There has to be a better organized way where I can have a separate class for functionality but wont I then have to create a new object for each test?
Sorry about the confused post I'm trying to learn Java thoroughly and selenium also.
EDITED
I have copied the process of creating a page object hybrid model that is documented in the YouTube video:
https://www.youtube.com/watch?v=gxwh8D_tx-0
I created a Pages package which contains all of the Page specific class such such ProfilePage, NotificationPage etc...
I have a second package which contains the tests and a testbase class which generates the driver object, opens the browser.
I want to get to the stage where in my tests class I can have a specific class for a test for example:
class test_that_user_can_upload_profile_picture
When I create such a class I have methods inside the class such as: test_that_navigation_to _profile_page_successful()
test_to_upload_valid_picture()
Should such navigation methods be inside this class?
Also I find that in order to access my methods from a package I need to mark my methods as static. Is this ok? I noticed on the youtube video the instructors methods were not static. Looking at the setup I dont quite understand why I cant access the methods unless I mark them as static. The error i get is
"Cannot make a static reference to a non-static method"
Here is my setup:
Also Im finding that in my ProfilePageNavigation class I have a bunch of methods that run in a specific order based alphabetical order.
Is it simply the case I should just have 1 method in each test class and just call the page classes methods(or any other pertinent class) to execute this test? If it is just 1 method inside each test class then wouldnt I have too many test classes each with a name like (for example) upload_valid_profile_picture with a method using the same name? and then another class with upload_invalid_profile_picture with it's method. I dont want to go down that path - how do I resolve that?
Also all my Pages class methods have to take WebDriver driver as a parameter is there any way around this - it is a lot of duplication.
If you could point me on the right track and let me know in it is ok to have the pages class methods as static it would be appreciated.
I guess I just want to know whether I am on the right track or going down the wrong route at this early stage.
#tarquin - you can find many articles on that #web, There are multiple ways to handle your code and work with it, my way is :
Create a objects repository in notepad or excel, from where you can pick/change/manage all your objects.
Create a class with all re-usable methods.
Create a class of your tests.
Thanks
Keshav

When to Stop Defining FindBy's in the Object and Move Them to the Test

So I have an interesting conundrum I was curious to get some feedback from other Webdriver framework architects. Currently I follow a pretty standard execution model:
baseobject
pageobject (extends baseobject)
Junit testobject (references one or multiple pageobjects)
Within my pageobjects I chose to define my findBy UI mappings as variables, and in-turn reference them within the various methods I write for that pageobject. I find this works very well. However, one item I am waffling on is how to handle method design for pages (and their respective pageobject) when there exist potentially 50 separate hyperlinks.
My inclination and design thus far has been to create methods (I think of them as services really) for each link on most pageobjects I've created so that #Test I can simply call the method I want and be done with it. This eliminates the potential for test maintenance...standard practice I know. But I am now trying to decide...does it make sense to create 50 methods, one for each link for a page object, or do I go against my wishes and pass in linktext from the test itself, feeding into a single method that builds the findBy using that passed in parameter.
On one hand there is way less code within the pageobject, but on the other, tests become more brittle. There is potential for these links to be references in hundreds of tests.
Here is a brief example of my model:
classname extends baseobject{
By someLocator = By.linkText("some text");
By someOtherLocator = By.linkText("some other text");
By andAnotherLocator = By.id("someid");
public void someLinkMethod(){
driver.findElement(someLocator).click();
}
public void someOtherLinkMethod(){
driver.findElement(someOtherLocator).click();
}
public void someidMethod(){
driver.findElement(andAnotherLocator).click();
}
}
Thus we come to the end of the question. This model works great for test design. My services (methods) are insulated and easily maintainable. But what would I do if there were 50 UI mappings for links instead of 2 as I have shown above? I toyed with the following design, but really dislike it #Test:
public void selectFromLeftBar(String barItem){
driver.findElement(by.linkText(barItem)).click();
}
Any thoughts would be greatly appreciated!
Do it in your page object class. Here are the reasons:
What does your code do if your page changes the link text? You have to go into each test and change that text, even if the link does the same thing.
What happens if your page removes that link? You are stuck with the same problem, namely, having to find each time you call that link. If its a method...then you delete the method, and your IDE notifies you of each instance that you used it.
Finally, you are providing a standard interface for the test. If you make an exception here, what would stop you from passing other things into your page?
As a side note, I would recommend only mapping elements that you are going to use. I've found that if I map out every element I could possibly ever need then I end up with a massive class filled with fluff and less time on my hands.

Howto access a string value from another class?

I have a class that consists of an edittext box to hold a string "strResponse" I want to access this string in another class. Basically a user types in whatever, and the other class takes that string, and sends it in a text message.
That code works fine if I hard code it in. But I want a user to define what the message should say from the other class.
I was thinking global variable, but have had no luck so far.
what do you mean by hardcode it? could you post your code here?
normally, a decent oop design isn't class 1 "takes" values from class 2, if class 2 is the one which is taking inputs from the user.
The design should be something, class 2 takes input and once input is done it sends/flag/notify class 1 for updated value.
sends/flag/notify could be done by different ways. depends on the design.
I think you need to access the string value of dialog box class to the main Activity class. (Just a for example)
You need interface to do this. i Think link will help
http://developmentality.wordpress.com/2009/10/31/android-dialog-box-tutorial/
In the above link string value in the dialog class is accessed in main activity call using the interface.
Hope this will solve your issue.

Displaying class contents in a GUI with Swing

I have some classes with fields and getters/setters and I want to display the fields of the class in a Swing application.
The name of each field should be dosplayed in a Label and the value should be displayed depending on the type of the value, i. e. String uses a TextField, List uses a ComboBox and so on.
One difficulty is that the class can contain fields, which also need to be treated this way itself.
Is there a recommended/standard way of doing that?
I looked a bit into java.beans but I'm not really sure if it isn't primarily used when the class is a Swing component itself.
Another option is to use PropertySheet component from L2FProd. From you description it should do exactly what you wanted.
It wasn't updated for a long time but works well.
I don't think there is an automated way to accomplish this. In fact I think you might even be approaching it incorrectly. What if you List contains objects other than Strings for example, how is a JComboBox going to break this out? I would suggest just using a JTable and putting the name in the first column and the value in a second column.
You could add #Annotations to your fields you want to examine and display in the GUI. In every case you have to use reflections to access and find all the fields you want to display. I think you have to add a custom logic to the databinding which examines a given class for it's fields (maybe filtered with a #Display annotation or stuff like this).
This should be very easy to implement, you can rely on the POJO Bean definition and access all the fields over getter/setter, don't forget to cache the handled classes to avoid circular dependencies of classes.
I hope this helps.
There is ReflectionUI. It can display primitive values objects and lists by just using reflection. It works well with getter/setter properties.

Categories

Resources