Using assertEqual and getElementbyxpath in Junit Selenium - junit

I'm basically writing a testcase to determine if a status shared on Facebook was actually shared. Below is the xpath for getting the text of first post on Facebook. I want to compare it to the status I posted e.g 'Blah'. I've been trying to use AssertEquals but that doesn't seem to be working.
WebElement status = getElement(By.Xpath("//div[#id='pagelet_home_stream']//ul[#id = 'home_stream']//li[1]//span[#class='userContent']")
AssertEquals(status, "Blah");

You need to call element.getText() otherwise you're comparing with a WebElement object rather than a String. Also, it's important that the expected text goes first in the assertEquals(expected, actual) otherwise you get a confusing message when they don't match.
assertEquals("Blah", status.getText());

Related

Reading RawRequest JSON parameter value in SoapUI changes its value

I'm trying to transfer parameter from RawRequest using SoapUI but when reading it, value changes.
The parameter is request ID (which is unique for every test), it is requested by every test case from Custom Properties, where it is stored as follows:
${=((System.currentTimeMillis().toString()).subSequence(4, (System.currentTimeMillis().toString()).length())).replaceFirst("0", "")}
Above generates number like this for example:17879164.
The problem starts, when I'm trying to transfer it using either in build in feature or Groovy script. Both read parameter incorrectly:
Following is how the parameter presents in RawRequest window:
This is how it is read in Transfer window in SoapUI:
And finally, how it is read by Groovy script:
Can any one explain, why this value despite being shown in SoapUI RawRequest window as 17879164 is then read as 17879178 using two different methods?
I think the clue might be, that when I'm using "flat number" as reqId and not the generated one, both methods work fine and return correct number. But in this case when it is RawRequest, I understand that it is set once and for all, so what is show in the window and what is being read, should be the same.
What you are seeing is a "feature" in SoapUI. Your transfer step will transfer the code, which will then get evaluated again, resulting in a different value.
What you need to do is:
Create a test case property.
Set the property from test case setup script to a value. So in your case, something like testCase.setPropertyValue("your_property", ((System.currentTimeMillis().toString()).subSequence(4, (System.currentTimeMillis().toString()).length())).replaceFirst("0", ""))
Anywhere in your test refer to the test case property ${#TestCase#your_property}... which is a fixed value at this point, so will be always the same.

Junit: String return for AssertEquals

I have test cases defined in an Excel sheet. I am reading a string from this sheet (my expected result) and comparing it to a result I read from a database (my actual result). I then use AssertEquals(expectedResult, actualResult) which prints any errors to a log file (i'm using log4j), e.g. I get java.lang.AssertionError: Different output expected:<10> but was:<7> as a result.
I now need to write that result into the Excel sheet (the one that defines the test cases). If only AssertEquals returned String, with the AssertionError text that would be great, as I could just write that immediately to my Excel sheet. Since it returns void though I got stuck.
Is there a way I could read the AssertionError without parsing the log file?
Thanks.
I think you're using junit incorrectly here. THis is why
assertEquals not AssertEquals ( ;) )
you shouldnt need to log. You should just let the assertions do their job. If it's all green then you're good and you dont need to check a log. If you get blue or red (eclipse colours :)) then you have problems to look at. Blue is failure which means that your assertions are wrong. For example you get 7 but expect 10. Red means error. You have a null pointer or some other exception that is throwing while you are running
You should need to read from an excel file or databse for the unit tests. If you really need to coordinate with other systems then you should try and stub or mock them. With the unit test you should work on trying to testing the method in code
if you are bootstrapping on JUnit to try and compare an excel sheet and database then I would ust export the table in excel as well and then just do a comparison in excel between columns
Reading from/writing to files is not really what tests should be doing. The input for the tests should be defined in the test, not in the external file which can change - this can either introduce false negatives or even worse false positives (making your tests effectively useless while also giving false confidence that everything is ok because tests are green).
Given your comment (a loop with 10k different parameters coming from file), I would recommend converting this excel file into JUnit Parameterized test. You may want to put the array definition in another class, because 10k lines is quite a lot.
If it is some corporate bureaucracy, and you need to have this excel file, then it makes sense to not write a classic "test". I would recommend just a main method that does the job - reads the file, runs the code, checks the output using simple if (output.equals(expected)) and then writes back to file.
Wrap your AssertEquals(expectedResult, actualResult) with try catch
in catch
catch(AssertionError e){
//deal with e.getMessage or etc.
}
But it not good idea for some reasons, I guess.
And try google something like soft assert
Documentation on assertEquals is pretty clear on what the method does:
Asserts that two objects are equal. If they are not, an AssertionError
without a message is thrown.
You need to wrap the assertion with try-catch block and in the exception handling do Your logging. You will need to make Your own message using the information from the specific test case, but this what You asked for.
Note:
If expected and actual are null, they are considered equal.

Junit test cases for a class that generates text content

I have a class named EmailNotificationContentBuilder. As the name suggest the class is responsible to generate content for an email notification to be sent after a process ends. The notification basically tells whether the process was successful or not, the start time end time and the statuses of the child processes ( in tabular format ). I have following doubts regarding writing Junit test cases for this class:-
Is it required to have a Junit for this class? Since it generates textual content.
If yes then how can I assert the content generated by the class ? Some of the contents are represented in tabular format.
Do you want to make sure it does what it's supposed to do? If yes, then write a test. If you don't care if the code works fine or not, then don't write one.
This is the most typical thing a unit test does: test that the value returned by a method is correct. Get the String it returns, and check that it's what you expect it to be:
#Test
public void shouldReturnTabularData() {
EmailNotificationContentBuilder builder = new EmailNotificationContentBuilder();
String result = builder.build("some input");
assertEquals("title1\ttitle2\nvalue1\tvalue2", result);
}

Three rows of almost the same code behave differently

I have three dropdown boxes on a Main_Form. I will add the chosen content into three fields on the form, Form_Applications.
These three lines are added :
Form_Applications.Classification = Form_Main_Form.Combo43.Value
Form_Applications.Countryname_Cluster = Form_Main_Form.Combo56.Value
Form_Applications.Application = Form_Main_Form.Combo64.Value
The first two work perfectly but the last one gives error code 438!
I can enter in the immediate window :
Form_Applications.Classification = "what ever"
Form_Applications.Countryname_Cluster = "what ever"
but not for the third line. Then, after enter, the Object doesn't support this property or method error appears.
I didn't expect this error as I do exactly the same as in the first two lines.
Can you please help or do you need more info ?
In VBA Application is a special word and should not be used to address fields.
FormName.Application will return an object that points to the application instance that is running that form as opposed to an object within that form.
From the Application object you can do all sorts of other things such as executing external programs and other application level stuff like saving files/
Rename your Application field to something else, perhaps ApplicationCombo and change your line of code to match the new name. After doing this the code should execute as you expect.
Form_Applications.Application is referring to the application itself. It is not a field, so therefore it is not assignable (at least with a string).
You really haven't provided enough code to draw any real conclusions though. But looking at what you have posted, you definitely need to rethink your approach.
It's to say definitely but you are not doing the same. It looks like you are reading a ComboBox value the same (I will assume Combo64 is the same as 43 and 56) but my guess is that what you are assigning that value to is the problem:
Form_Applications.Application =
Application is not assignable. Is there another field you meant to use there?

How do I work around cases where the listProvider data comes back as just ONE single record?

Im using HTTPservice to load XML and show the results in a list - it works when there are more than 1 XML record found.... but when there is only ONE single returned XML record it gets treated differently for some reason and generates this error:
Suspended: TypeError: Error #1034: Type Coercion failed: cannot convert mx.utils::ObjectProxy
I see posts like this:
http://anupushkaran.blogspot.com/2010/02/typeerror-error-1034-type-coercion.html
but I cant figure out how to adapt it to my HTTPService resultHandler code block...
Im using FB 4.6 and my XML structure looks like the following:
SiteXYZ
Events
EventListing
and all the data I want to use is under the EventListing Node.
I've tested some code that can detect when the length of whats returned so I think I can just detect when the length is 1 and then handle something differently -- not sure what though.
Another thing Im seeing that I think gets me close... when I look at the network monitor's TreeView > Response > Response Body I can see that on a succesful trip, the body comes back like:
SiteXYZ
Events
EventListing
[0]
[1].... and so on....
But on the cases where its a SINGLE record returned, the body of the response comes back like:
SiteXYZ
Events
EventListing
so shouldnt I be able to detect when its just ONE returned record and then set the list dataProvider accordingly? so that when its a lot of returned records the dataprovider is set with:
list1.dataProvider = myXML.lastResult.SiteXYZ.Events.EventListing;
but when its just ONE returned record, how would that dataProvider be set? this didnt work:
list1.dataProvider = lfXML.lastResult.LeisureFun.Events;
can anyone help with this? is my approach way off base? sorry for the rambling nature of the question but as I typed it, I stopped about 5 times and tried a number of things that came to mind as I thought through it... still nothing worked though... I feel like Im onto something (could be wrong though) but just cant solve it yet.
Answer was in the original website I referenced....
i took a closer look at what that post said the problem presented was and how that code snippet addressed it and was able to get it to work for me by putting it in my resultHandler block...
arr=new ArrayCollection();
if(event.result.SiteXYZ.Events.EventListing is ArrayCollection)
{
arr = ArrayCollection(event.result.SiteXYZ.Events.EventListing);
}
if(event.result.SiteXYZ.Events.EventListing is ObjectProxy)
{
arr =new ArrayCollection(ArrayUtil.toArray
(event.result.SiteXYZ.Events.EventListing));
}
now when there is one record returned it shows fine... same for multiple records returned.
thanks for the help though... I'll definately save this snippet for future use!