I need to test a dynamic Web Element using Selenium that will change its value after some time(back-end dependent). So, I built a boolean method that returns true if the web element has the value I need and false if that value is never retrieved. I want to check for value change at some intervals (thus, the Thread.sleep between page refreshes). My code always returns true, what am I doing wrong?
public boolean checkStatus() throws InterruptedException {
for(int i=0; i<2;) {
if (!serviceStatus.serviceElement().equals("LIVE")) {
Thread.sleep(5000);
theBrowser.navigate().refresh();
i++;
}else{
return true;
}
}
return false;
}
The method is used in the main test on the following assert:
Ensure.that(checkStatus()).isTrue();
I presume serviceElement() is the Webelement,you have to get the text for the element using getText() method and compare with the string you desire("LIVE") in this scenario.You seems to be comparing an Webelement with String here. Below should work if i understood the requirement correctly.
public boolean checkStatus() throws InterruptedException {
boolean isMatching = false;
for(int i=0; i<2;) {
if (!serviceStatus.serviceElement().getText().equals("LIVE")) {
Thread.sleep(5000);
theBrowser.navigate().refresh();
i++;
isMatching = false;
break;
}else{
isMatching = true;
}
}
return isMatching;
}
Related
I need help with this scenario where I need to find a string from a pagination table wherein each page contains 50 items. My code below works fine, only problem is that when it cannot find the data my while loop sometimes keep running indefinitely and does not fail but sometimes it does! What can I do so that it will always return an error after reaching a number of loops?
public int inboxLocateLoan(String expName, String name) throws Throwable {
//Locate Loan element in SharePoint table
report.setFailedResult("Loan element is not found");
int loanRow;
try {
boolean loansearch = true;
while (loansearch) {
List < WebElement > rowElem = getWebElements(getAEDriver(), "xpath", sRow);
for (int i = 1; i <= rowElem.size(); i++) {
String actualLoanName = driver.findElement(By.xpath("//*[#id='onetidDoclibViewTbl0']/tbody[2]/tr[" + i + "]/td[3]")).getText();
// String actualLoanNumber = driver.findElement(By.xpath("//*[#id='onetidDoclibViewTbl0']/tbody[2]/tr["+i+"]/td[5]")).getText();
loanRow = i;
if (actualLoanName.equals(expName)) {
loansearch = false;
return loanRow;
}
if (actualLoanName.equals(name)) {
click(getAEDriver(), "xpath", "//*[#class='ms-pivotControl-surfacedOpt-selected']", "Refresh");
loansearch = true;
} else {
if (i == 50) {
click(getAEDriver(), "xpath", "//*[#id='pagingWPQ2next']/a", "Next Page");
} else {
loansearch = true;
}
}
}
}
}
Initialize the romElem outside the for, and then use it to toggle your flag. If you reached your max rowElemen and you didn't find what you were looking for, it is safe then to assume that the value will be false.
Also, what is the purpose of the while? you could remove it completely, it is usually a bad idea to mix a while and a for. I donĀ“t see the point in doing so in this case.
I am trying to use isDisplay() method in my selenium script.
While I am using this method I have found this method returns false every time.
Is there any other way to use this method or alternative of this method?
Scroll_UP is done successfully. But If element is display, Scroll_UP is not terminate.And I need return value also.
Thanks in advance.
code logic which I write
public static boolean scroll_Up_Until_FindWebe(WebElement dragEle, WebElement ele, int scrollPoint)
{
int numberOfPixelsToDragTheScrollbarUp = -10;
for (int i = scrollPoint; i > 10; i = i +numberOfPixelsToDragTheScrollbarUp)
{
if (ele.isDisplayed()) //if the tag options is displayed
{
Log4J.logp.info("Ending scroll_Up_Until_FindWebe - Element is found");
return true;
}
dragger.moveToElement(dragEle).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarUp).release().build().perform();
}
Thread.sleep(500);
return scroll_Up_Until_FindWebe(dragEle, ele, 30);
}
The method is not returning the value of the local variable.
Can I use the value of local variable index from the following method
public boolean contains(Object input) {
int index = 0;
while(myAsetIterator.hasNext()) {
index++;
if(input.equals(myAsetIterator.next())) {
return true;
}
}
return false;
}
in this method as the index of the array of the object that I want to remove.
public boolean remove(Object o) {
int count = 0;
if(o == null) {
return false;
}
if(contains(o)) {
genArray[index] == null;
}
if (count > 0) {
System.out.println(count+" same elements were present in Aset. "
+ "Removed all those "+count+" elements from Aset.");
return true;
}
return false;
}
I know the scope of a local variable is limited to the method it's declared in. But there might be a way that I might not now yet to make this happen without using a field/instance variable.
No. The whole point of it being local to a method is that it only exists within that method. The options are:
Use an instance field, i.e. make it part of the state of the object. That's unlikely to be appropriate.
Use a static field, i.e. make it part of the static of the type. That's almost certainly inappropriate.
Change the existing method to return the information you want.
Create a new method to return the information you want.
Duplicate the existing code within remove so that you can get the index. That would be sad :(
As an example of the last two, you could write:
public int indexOf(Object input) {
int index = 0;
while(myAsetIterator.hasNext()) {
index++;
if (input.equals(myAsetIterator.next())) {
return index;
}
}
return -1;
}
public boolean contains(Object input) {
return indexOf(input) == -1;
}
... then in your remove method, you'd use indexOf instead of contains.
I m trying to create a method that searches the content of a list called authors looking for the element (the author's name) given as arg, and then deletes the specific field. If successful it returns true. But it won't work for some reason. I believe the error lies in the authors.remove(authorName); because the main class will not erase anything when given the order Book.removeAuthorByName("White");.
public boolean removeAuthorByName(String authorName){
boolean val = authors.contains(authorName);
for (int i = 0; i <= numAuthors; i++){
if(val = true){
authors.remove(authorName);
authors.trimToSize();
}
}
return val;
}
public int listSize(){
return authors.size();
}
Little more concise and better will be
public boolean removeAuthorByName(String authorName){
int index = authors.indexOf(authorName);
if(index > -1){
authors.remove(index);
return true;
}
return false;
}
First of all, you should check whether your list supports the remove(Object) method; it does because otherwise you would have received an UnsupportedOperationException.
You can simply have to call authors.remove(name); the method will return true if the passed name was found and deleted successfully - see the documentation.
Remove the while loop check using if clause. Also check case sensitivity
if(list.contains(authorname)){
list.remove(authorname);
return true;
}
else
{
return false;
}
I am interested in a very simple string verification problem to see if the starting character in a string starts with an upper case letter and then have the console to display true or false. From my understanding you wouldn't have to invoke something like System.console().printf("true", s) in order to make this happen. I could swear I've seen similar elementary implementations achieved using the following sample code:
public class Verify {
public static boolean checkStartChar(String s) {
if (s.startsWith("[A-Z]")) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
String str = "abCD";
checkStartChar(str);
}
}
but when I run this, nothing displays. If I make a slight modification by adding in conditional printouts before returning T/F, e.g.
public class Verify2 {
public static boolean checkStartChar(String s) {
if (s.startsWith("[A-Z]")) {
System.out.println("yep");
return true;
}
else {
System.out.println("nope");
return false;
}
}
public static void main(String[] args) {
String str = "abCD";
checkStartChar(str);
}
}
the issue is somewhat resolved, as the console displays either "yep" or "nope", yet unresolved because I just want the console to display true or false. That's it. Advice?
As the question has already been answered, I'd like to point out there is no need for RegExes to solve this (and they are expensive operations). You could do it simply like this:
static boolean startsWithUpperCase(String toCheck)
{
if(toCheck != null && !toCheck.isEmpty())
{
return Character.isUpperCase(toCheck.charAt(0));
}
return false;
}
yet unresolved because I just want the console to display true or false
Calling checkStartChar method will return value, that doesn't mean it will print value to console. You need to code how you would like to handle return value. If you want to print return value, then you should do:
System.out.println(checkStartChar(str));
Will print what ever the return of checkStartChar method
if(s.startsWith("[A-Z]")){
String.startsWith(prefix) doesn't take regex as a parameter, you should be using regex APi instead.
Pattern p = Pattern.compile("[A-Z]");
Matcher m = p.matcher(new Character(s.charAt(0)).toString());
if(m.find()){
return true;
}else{
return false;
}
String str = "AbCD";
System.out.println(checkStartChar(str));
Output:
true
In your code checkStartChar(str); is returning a boolean value which is not being used in your program.Then if you want to display true or false then you can use.
System.out.println(checkStartChar(str));