I have an input file which contains some String values. Lets Say String one.
On a web page, I am identifying a web element and getting its value as a String. lets say String two.
String one = "abc" //Value from input file.
String two = "USabc" // Value from identifying a web element.
I have to compare these 2 strings and my test case is that String two should contain "US" as well as contents of String one which are "abc"
How can i do this ?
Also, if I have multiple inputs and multiple outputs in one script, whats the best way of doing this?
To compare the presence of the text within the variable one as well as the text US within the variable two you can use the following solution:
if(two.contains(one) && two.contains("US"))
System.out.println("Comparision successful");
else
System.out.println("Comparision unsuccessful");
Selenium Assertions are the best way you can use Assert to compare two values.
Assertions verify that the state of the application is same to what we are expecting. Selenium Assertions can be of three types: “assert”, “verify”, and ” waitFor”. When an “assert” fails, the test is aborted. When a “verify” fails, the test will continue execution, logging the failure.
Available in
import org.testng.Assert;
and perform
Assert.assertEquals("one","two");
If you do not want your test to fail at a particular assertion and want to continue till the end and then assert, you can use SoftAsserts
You can read more about that here https://www.seleniumeasy.com/testng-tutorials/soft-asserts-in-testng-example
Related
I'm looking for a flexible/generic way to build up conditions using metadata stored in a Database and then validate incoming requests at runtime
against the conditions and concatenate value(s) if the condition is met.
My use case looks something like this:
1) A business user selects an operation from a UI i.e. (IF condition from a dropdown), then selects an appropraite field to evaluate i.e. ("language")
then selects a value for the condition i.e. "Java" followed by some values to concatenate i.e "Java 9" and "is coming soon!"
2) This metaData will get stored in a Database (lets say as a List for the moment) i.e ["language","Java","Java 9","is coming soon"]
When my application starts I want to build the appropriate concatenation conditions:
private String concatenateString(String condition, String conditionValue, String concatValue1, String concatValue2){
StringBuilder sb = new StringBuilder();
if (condition.equals(conditionValue)){
sb.append(concatValue1);
sb.append(concatValue2);
}
return sb.toString();
}
3) so at runtime when I receieve a request, i want to compare the values on my incoming request to the various conditions that got built at start up:
if language == "Java" then the output would look like => "Java 9 is coming soon"
While the above might work for 2 String concatenations, how can achieve the same for a variable number of conditions and concatenation values.
So you want user to create a program by selecting options from a GUI which will be stored in a DB. When the options are read back from the DB you want to parse this into a compileable program and run it?
Use StringBuilder to build a string of the code from the data gotten back from the DB, something like this:
"if (language == '"Java"') { doSomething() }" (you'll need to take care to escape strings inside your string if you are storing strings in the DB.
You can then use Compiler class to compile the string to a program which yo can run (all in runtime, google dynamically compiling c# at runtime).
However, you'll probably want to question why you are thinking of going down that route... I've been there before, dynamic compilation has a very narrow use case.
You could, for instance, create a Dictionary which maps selected languages to some output string and simply use this to get your output like:
Dictionary<string, string> langaugeOutputMap = new Dictionary<string, string>();
languageOutputMap.Put("Java", "Java9 is coming soon");
private string concatString(string: userChosenString) {
if (languageOutputMap.containsKey(userChosenString) {
return languageOutputMap.getValue(userChosenString);
}
return string.Empty()
}
If you then want to manage multiple conditions, you could have multiple Dictionaries for each condition type and enumerate them in a collection, iterate over them when given a variable sized set of conditions and make sure that all the conditions evaluate through the use of containsKey().
Also, you can use params to specify variable length function arguments like so:
public string manyArgs(params string[] stringArgs) {
}
Also, look at PredicateBuilder:
http://www.albahari.com/nutshell/predicatebuilder.aspx
Lets see the below scenario.
On the registration form, there is field called "Hobbies" which has three check boxes "Reading", "Dance" and "Cricket".
I have to select two check boxes "Reading" and "Cricket". I have to pass one string value (e.g. "Reading,Cricket") to one method say "selectMultipleCheckboxes". So, based on passing value, it should split the string and it should select two check boxes.
Note: I don't want to select single check box or all check boxes.
Could you please help me to write test script?
I'm not really an expert on Java, but since someone just answered my question, I thought I would at least try to help you out.
You would do something like this:
String originalText = "Reading,Cricket";
String[] parts = originalText.split(",");
String readingPart = parts[0];
String cricketPart = parts[1];
Then you would pass those strings to Selenium:
driver.findElement(By.id(cricketPart)).click();
That would of course be assuming your checkbox's DOM ID was called "Cricket"
Suppose there is a string abc and given rotational strings like abc, bca ,cab(i.e 3 possible rotational strings are possible) similarly i need a method that should take two string as input and tell whether those two follow under this category or not.
I thought of the following:
given string abc
string length =3
created a big array abcbcacab
and check using contains.
in this solution i have problems
as wrong input also get passed i.e "cbc"
Simple solution:
Test string length (after rotation, the string-length would still be the same).
Concatenate the string to itself (either works) and test wether the result contains the other one.
If both tests passed, the two strings are rotations of each other.
I have a loop in my code which goes over a set of strings. Said strings are then passed along to several other functions.
In my tests, I'm basically emulating the flow of code and asserting at the start of each method I expect it to visit, if the string is correct.
But I can only write the test method once. Meaning that I have to do the following to catch all the different strings:
assertTrue(string.equals("test1") || string.equals("test2") || string.equals("test3") || ...);
However, there's a problem with this if one or more of those strings are not successfully passed to the list that is looped over. Since this is a chain of OR statements, it will succeed as long as there is 1 correct string, regardless of whether any of the other strings are missing. Which is a problem.
I can't emulate the loop, I can only emulate the functions receiving the data each time.
Is there a way to deal with this problem?
EDIT: some clarification.
I start out with a list of strings.
This list gets looped over, meaning every single string instance will go through a bunch of functions. And then the next string. And so on.
In the test, I can write dummies for the methods the string goes through. This means I override the behavior of the actual code and send my own custom return. This has to be correct though, since the function following that has to properly process what I just send to it.
But, when I start the test with the dummy data, it will do the loop, meaning the same function gets called multiple times, each time with a different string. I can't just do 1 test for one of the strings, because the next loop will fail on the next string.
I'm being asked to sketch a Junit test case for a method:
public int checkOutItems(ArrayList<int> trolley)
which will return an ArrayList of item name and price list.
It is also defined the types of test case like normal condition,boundary, and exceptional.
I'm just wondering what is meant by boundary and exceptional?
Normal will just be to compare the input and output but boundary and exceptional?
Boundary tests
Let's say you need to test something that adds up to 50 numbers together. Your first boundary test would be to pass a list containing no items, the lower boundary. Your next test would be to pass a list that contains 50 numbers, the upper boundary. Boundary tests are often easy to write as you try to test the maximum or minimum (or a combination of them) parameter values. These should all work, of course, otherwise they belong in the next section.
Exception tests
What will happen if you pass a null list to a method? What happens if your list contains 51 items instead of the 50 maximum? What about passing in a list of Strings instead of Integers, for example? Exception tests are designed to try and break the program, either to test known limitations, or to test exception handling.
Boundary test - pass boundary valid arguments (like pass empty array to length function and check that result is zero)
Exceptional test - pass invalid argument and check that appropriate exception is raised.