As we know, assert continues the execution but verify stops the execution the moment the script fails.
e.g suppose two string abc, abd xyz
i want to verify the two strings. How to verify them without using Assert.assertEquals(expected, actual);
Can anyone please guide me for the same?
You can verify the 2 strings by creating a method and using if else to compare them.
In your example instead of assert this can be done
public void compareMethod()
{
string expected = "abc";
string actual = "xyz";
if(expected == actual)
{
//do steps required
}
else
{
}
}
If you have already stored strings and want to verifyText using Selenium WebDriver then the following code will surely help..
if("expected String".equals("actual string"))
{
System.out.println("String matches ");
}
else
{
System.out.println("String does not match ");
}
Try this
Related
// Language Selection
public static void SelectLanguage() {
waitForElementToBeClickable(driver.findElement(By.xpath("//div[#class=\"lang-identifier\"]")));
driver.findElement(By.xpath("//div[#class=\"lang-identifier\"]")).click();
List<WebElement> elements = driver.findElements(By.xpath("//ul[#class=\"dropdown-menu pull-right\"]/li"));
for (WebElement e : elements) {
String text = e.getAttribute("value");
System.out.println(e.getText());
if (text.equalsIgnoreCase("English")) {
e.click();
break;
} else if (e.getText().equalsIgnoreCase("Español")) {
e.click();
break;
} else if (e.getText().equalsIgnoreCase("Italiano")) {
e.click();
break;
} else if (e.getText().equalsIgnoreCase("Pусский")) {
e.click();
break;
} else if (e.getText().equalsIgnoreCase("Français")) {
e.click();
break;
} else if (e.getText().equalsIgnoreCase("Português")) {
e.click();
break;
} else {
System.out.println("Please select appropriate language");
}
}
}
I would suggest a much simpler but more flexible version of your method.
Some suggestions:
I would change your method to take the desired language as a parameter to significantly simplify the code but also make it very flexible.
WebDriverWait, in most cases, will return the found element(s). Use that to simplify your code to a one-liner, e.g.
new WebDriverWait(...).until(ExpectedConditions.elementToBeClickable).click();
You didn't provide the code of your custom method, waitForElementToBeClickable, but if you really want to keep it, have it return the element(s) waited for to make it more useful and save having to write extra code.
If you have nested double quotes, I would suggest you use a combination of double and single quotes. It's a personal preference but I think it makes it easier to read than \", e.g.
"//div[#class=\"lang-identifier\"]"
would turn into
"//div[#class='lang-identifier']"
Instead of grabbing all options and then looping through them to compare the contained text to some desired string, use an XPath that contains the desired text instead, e.g. for "English" the XPath will look like
//ul[#class='dropdown-menu pull-right']/li[text()='English']
NOTE: .getAttribute("value") gets the value of an INPUT and will not work on other elements, e.g. the LI elements in your elements variable. .getText() returns the text contained in an element but will not work on INPUTs.
After implementing these suggestions, the code turns into a two-liner and is very flexible.
public static void SelectLanguage(String language) {
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.lang-identifier"))).click();
driver.findElement(By.xpath("//ul[#class='dropdown-menu pull-right']/li[text()='" + language + "']")).click();
}
I'm trying to adapt KG4SGP's RTTY modulator to send a textfile instead of a fixed char array. I'm getting Strings by the readLine() method, altering these Strings by replacing certain values and then copy the altered String to a charArray.
I changed his globalchar msg[] = "\n\nCQ CQ CQ DE KG4SGP KG4SGP KG4SGP KN\n\n"; tochar msg[70]; and hoped to have this running instead;
while(text.available()) {
String lin= readLine();
lin.toLowerCase();
if (lin.indexOf("&")) {
if (lin.indexOf("&de ")) {
lin.replace("&de ", "de ");
result = calculateDayOfYear(dag,maand,jaar);
lin.replace("juldate" , String(result));
lin.replace("hour", String(uur));
lin.replace("min", String(mins));
}
if (lin.indexOf("month")) {
lin.replace("&","");
lin.replace("day", String(dag));
lin.replace("hour", String(uur));
rmin=random(1, mins);
lin.replace("rndmin", String(rmin));
lin.replace("month", (months[maand-1]));
lin.replace("year", kortjaar );
}
lin.toUpperCase();
lin.toCharArray(msg,lin.length()+1);
//Serial.println(msg);
}
Well, whatever I did or tried, I'm never getting any charArray to correctly identify the current character and translate it.. :-( It seems to all come out of the timerfunction
All suggestions welcome
String color1 = moreColors.get(0);
String color2 = moreColors[0];
System.assertEquals(color1, color2);
// Iterate over a list to read elements
for(Integer i=0;i<colors.size();i++) {
// Write value to the debug log
System.debug(colors[i]);
}
I am learning Apex and just started what is meaning of line System.assertEquals(color1, color2); and what is mean by debug log here?
System.assert, System.assertEquals, System.assertNotEquals. I argue these are three of the most important method calls in Apex.
These are assert statements. They are used in testing to validate that the data you have matches your expectations.
System.assert tests an logical statement. If the statement evaluates to True, the code keeps running. If the statement evaluates to False, the code throws an exception.
System.assertEquals tests that two values are equal. If the two are equal, the code keeps running. If they are not equal, the code throws an exception.
System.assertNotEqual tests that two values are not equal. If the two are not equal, the code keeps running. If they are equal, the code throws an exception.
These are critical for completing system testing. In Apex Code, you must have 75% line test coverage. Many people do this by generating test code that simply covers 75% of their lines of code. However, this is an incomplete test. A good test class actually tests that the code does what you expect. This is really great to ensure that your code actually works. This makes debugging and regression testing far easier. For example. Lets create a method called square(Integer i) that squares the integer returned.
public static Integer square( Integer i ) {
return i * i;
}
A poor test method would simply be:
#isTest
public static void test_squar() {
square( 1 );
}
A good test method could be:
#isTest
public static void test_square() {
Integer i;
Integer ret_square;
i = 3;
ret_square = square( i );
System.assertEquals( i * i; ret_square );
}
How I would probably write it is like this:
#isTest
public static void test_square() {
for( Integer i = 0; i < MAX_TEST_RUNS; i++ ) {
System.assertEquals( i*i, square( i ) );
}
}
Good testing practices are integral to being a good developer. Look up more on Testing-Driven Development. https://en.wikipedia.org/wiki/Test-driven_development
Line by Line ...
//Get color in position 0 of moreColors list using the list get method store in string color1
String color1 = moreColors.get(0);
//Get color in position 0 of moreColors list using array notation store in string color2,
//basically getting the same value in a different way
String color2 = moreColors[0];
//Assert that the values are the same, throws exception if false
System.assertEquals(color1, color2);
// Iterate over a list to read elements
for(Integer i=0;i<colors.size();i++) {
// Write value to the debug log
System.debug(colors[i]);//Writes the value of color list ith position to the debug log
}
If you are running this code anonymously via the Developer console you can look for lines prefixed with DEBUG| to find the statements, for e.g.
16:09:32:001 USER_DEBUG 1|DEBUG| blue
More about system methods can be found at https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm#apex_System_System_methods
I'm trying to learn D and I thought after doing the hello world stuff, I could try something I wanted to do in Java before, where it was a big pain because of the way the Regex API worked: A little template engine.
So, I started with some simple code to read through a file, character by character:
import std.stdio, std.file, std.uni, std.array;
void main(string [] args) {
File f = File("src/res/test.dtl", "r");
bool escape = false;
char [] result;
Appender!(char[]) appender = appender(result);
foreach(c; f.rawRead(new char[f.size])) {
if(c == '\\') {
escape = true;
continue;
}
if(escape) {
escape = false;
// do something special
}
if(c == '#') {
// start of scope
}
appender.put(c);
}
writeln(appender.data());
}
The contents of my file could be something like this:
<h1>#{hello}</h1>
The goal is to replace the #{hello} part with some value passed to the engine.
So, I actually have two questions:
1. Is that a good way to process characters from file in D? I hacked this together after searching through all the imported modules and picking what sounded like it might do the job.
2. Sometimes, I would want to access more than one character (to improve checking for escape-sequences, find a whole scope, etc. Should I slice the array for that? Or are D's regex functions up to that challenge? So far, I only found matchFirst and matchAll methods, but I would like to match, replace and return to that position. How could that be done?
D standard library does not provide what you require. What you need is called "string interpolation", and here is a very nice implementation in D that you can use the way you describe: https://github.com/Abscissa/scriptlike/blob/4350eb745531720764861c82e0c4e689861bb17e/src/scriptlike/core.d#L139
Here is a blog post about this library: https://p0nce.github.io/d-idioms/#String-interpolation-as-a-library
I'm creating an Address Book program using array. I've done with the add and print data option. But now I'm stuck with the search/update option.
This is my code in searching for the element if it exist in my array or not.
public void update_data(){
String user_input_data;
int search_data = 0;
System.out.println("Enter the data that you want to search: ");
user_input_data = user_data.nextLine();
while(search_data<data_recorded){
if(user_input_data == AddressBook_Array_name[search_data])
{
System.out.println("Data found!");
}
else
{
System.out.println("Data not found!");
}
search_data++;
}
}
But when I run the program. It always return to false and print the else statement.
I don't know what's wrong with it. Anyway the data_recorded variable holds the number of data inputted by the user in the add option.
You need to use equals() instead of == in java for comparision.
if (user_input_data.equals(AddressBook_Array_name[search_data]))
Also, instead of the while you may want to use the foreach loop (removes the need for search_data variable).
for(String addressBookElem : AddressBook_Array_name) {
if (user_input_data.equals(addressBookElem)) {
System.out.println("Data found!");
return;
}
}
System.out.println("Data not found!"); // reaches this statement if data not present
i think you should use .equals function instead of ==.