condition:how To check data is displayed or not
Boolean isPresent3 = driver.findElements(By.cssSelector(".input-horizon.horizon-program-guide.form-control.ng-pristine.ng-untouched.ng-valid.ng-not-empty.ng-valid-min")).size()> 0;
if (isPresent3 == true) {
System.out.println("Aflam data exists");
}
else {
System.out.println("Aflam data does not exists");
}
Boolean isPresent3 = driver.findElements(By.cssSelector("csspath")).size()> 0;
this code some time works sometimes not any good solution that works evrytime
To check whether the element is visible use isDisplayed() method which returns boolean values (ie)
List<WebElement> element=driver.findElements(By.cssSelector(".input-horizon.horizon-program-guide.form-control.ng-pristine.ng-untouched.ng-valid.ng-not-empty.ng-valid-min"));
System.out.Println(element.size());
if(element.isDisplayed()==true){
System.out.Println("element is present");
}
else
{
System.out.Println("element is not present");
}
It checks whether the element is present on the page. To check visibility of the element you should use "isDisplayed()". My suggestion is to use both the conditions.
List<WebElement> elem= driver.findElements(By.cssSelector(".input-horizon.horizon-program-guide.form-control.ng-pristine.ng-untouched.ng-valid.ng-not-empty.ng-valid-min"));
if(elem.size()> 0){
if(elem.get(0).isDisplayed()){
System.out.println("Element is dislayed and present");
}
}
else{
System.out.println("Element is not present");
}
Well, if it works sometime and not all the time then it may be the case where the element takes time to be available in dom and your findElements statement executed before it's available!
Quick and dirty work-around is, add sleep, for example Thread.sleep(3000).
Anther work-around is you can have waitForElementpresent condition in try catch block and in catch block you can set flag false!
If you are using qaf you can use assertion/verification available with driver and element object where you don't need to use any of above work-arounds. For example in your case it may look like:
$("css=csspath").verifyPresent(); // checks only presence in dom.
$("css=csspath").verifyVisible();// ensures present and displayed
Related
We were given a task to make a program that takes the input of the user. there two types of input the user can use, 1st is the "Type in the Size" and the second is "Type in the style" either way the user can just input in the 1st field or the 2nd field. when the users clicks ok the two inputs will be use to sortout a arraylist which contains the type of size and style in it.
public void viewResult(String style, String size) {
style = style.toLowerCase();
size = size.toLowerCase();
new_list = new ArrayList<>();
for(Items_container items:current_arrayList)
{
if (items.getStyle().toLowerCase().contains(style) && items.getSize().toLowerCase().contains(size))
{
new_list.add(items);
break;
}
else if (items.getSize().toLowerCase().contains(size)) {
new_list.add(items);
break;
}
else if (items.getStyle().toLowerCase().contains(style)) {
new_list.add(items);
break;
}
}
current_arraylist.clear();
adapter.filterSearch(new_list);
if (new_list.size() == 0) {
results.setText("Search not found");
} else {
results.setText("Results");
}
}
this is the method that I use to sortout out the Items_container now it does work fine (I guess)
but the problem is for example the user inputs "large" in the size input field and "blazzing" in the style input field the program must sort the items_container using the given inputs but it is not working because the program also includes all the items that has the same size or the same style.
I tried adding a break to the loop but now it only shows one data and what if there two or more data that matches the givens inputs, how can I do that?
You should check first if both conditions are set. That way you can separate if either one matches and if both match. Maybe put singular matches in a separate list in case no items match both conditions, but that's up to you.
And as others already said, break stops the loop, continue moves to the next item.
like code below:
for (int i = 0; i <current_arrayList.size() ; i++) {
if(current_arrayList.get(i).getStyle().toLowerCase().contains(style)
&& current_arrayList.get(i).getSize().toLowerCase().contains(size))
{
new_list.add(current_arrayList.get(i));
//if used break ,stop loop
}
else if (current_arrayList.get(i).getSize().toLowerCase().contains(size)) {
new_list.add(current_arrayList.get(i));
}
else if (current_arrayList.get(i).getStyle().toLowerCase().contains(style)) {
new_list.add(current_arrayList.get(i));
}
}
current_arraylist.clear();
adapter.filterSearch(new_list);
adapter.notifyDataSetChanged();
Could somenone, please provide me with answer.
Namely, My for loop looks like (it actually founds element which has no atribute disabled, but what if all elements are disabled, then I want to finish test and sent message). So how can I rewrite this loop to be if/else.
if (disabled == null) {
dropdown3.selectByValue(option.getAttribute("value"));
break;
}
Message which I want to show as end of test should be:
ELement is not enabled, so finishing test
and go directly to
driver.close();
And finish test.
First, you can check for undisabled values
if (undisabled =! null) {
dropdown3.selectByValue(option.getAttribute("value"));
}
else {
System.out.print("All features are disabled.");
break;
}
I have a text box where I enter the text during tests, for example "cars", but very often not whole text appear in the text box, for example only "car". So my question is how can I wait until whole text will appear and how can I check that?
This
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.textToBePresentInElement(element, "text"));`
Doesn't work for me. It is the same result as without it.
[EDIT]
Thread.sleep(4000);
Doesn't work for me too.
Also
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(...).getAttribute("value").length() != 0;
}
});
Will not work for me, because there is no value, because it is not saved.
You are close, the only thing missing is a loop to catch Selenium TimeOut Exceptions whilst you are polling the element. Also, I would rather go with the isDisplayed() instead of isTextPresent() and focus my Xpath on locating the 'cars' text that you are after.
Here is the method for waiting, it will return true/or false based on if the element was present during the polling time (10sec for example); worth noting that if the element is found as present earlier than the 10sec limit the loop will break and will return true:
public boolean waitForElement(String elementXpath, int timeOut) {
try{
WebDriverWait wait = new WebDriverWait(driver, timeOut);
boolean elementPresent=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(elementXpath)).isDisplayed());
System.out.printf("%nElement is present [T/F]..? ")+elementPresent;
}
catch(TimeoutException e1){e1.printStackTrace();elementPresent=false;}
return elementPresent;
}
Now, it really boils down how you 'grab' the element. You mentioned, you want the full cars text to appear. Hypothetically speaking let's say the element is located by id=brand and you want the text inside an a href link. So you want something like this for your xpath:
//div[#id='brand']//a[text()[contains(., 'cars')]]
Please note that the above is case-sensitive so it will fail if you are looking for Cars.
Best of luck!
Update after OP's comment:
Correcting the way we identify the web-element:
//div[#id='widget_dijit_form_TextBox_0']//div//input
Now all is needed is to use the waitForElement above until the element appears. When it does you can grab the text by using:
String textInsideInputTag = elementPresent.getText();
Now you can compare this to your intended value (i.e. 'cars'):
if(textInsideInputTag.equals("cars")){
System.out.println("Successfully found cars inside <input> tag");
}
else{
System.out.println("Couldn't locate cars in the element!");
}
I want to check if HTML comment tags are inserted properly.. If there is the opening tag but no closing tag, i want to display error.
I referred this link and was able to retrieve valid comment nodes
for(Element e : document.getAllElements()){
for(Node n: e.childNodes()){
if(n instanceof Comment){
commentNodes++;
System.out.println(n);
}
}
}
if(html.contains("<!--") && commentNodes == 0) {
System.out.println("error");
} else if(html.contains("-->") && commentNodes == 0) {
System.out.println("error1");
}
Is there a better way of doing this?
I'm not sure whether JSoup will parse not closed comment tag, although there's another way to achieve your goal:
int openings = StringUtils.countMatches(html, "<!--");
int closures = StringUtils.countMatches(html, "-->");
if (openings > closures)
System.out.println("Error: There are not closed comment tags!");
It will count number of occurrences of comment tag openings and closures and by comparing it you can assume whether there is an not closed. To be entirely certain the result you can also compare the values with number of tags obtained by JSoup (commentNodes in your example).
I have a seperate JFrame where there is a text box (jTextArea) that takes numbers as inputs, each separated with a new line. Upon closing the JFrame with the text box, the data is supposed to be stored in an ArrayList of integers. The ArrayList is checked when clicking a button in the main JFrame and errors are logged if they happen.
The code for the JFrame with the jTextArea looks like this:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
boolean success = false;
try{
selectedTime = Long.parseLong(jTextField1.getText());
if(selectedTime >= 10000){
success = true;
if(!jTextArea1.equals("") && !jTextArea1.equals(null)){
try{
for(int i = 0; i < jTextArea1.getLineCount(); i++){
n = Integer.parseInt(jTextArea1.getText(jTextArea1.getLineStartOffset(i),jTextArea1.getLineEndOffset(i)));
if(n <= 172){
worldsChosen.add(n);
}
}
}catch(Exception e){
errorsHappened = true;
}
}
}else{
javax.swing.JOptionPane.showMessageDialog(null,"The specified time was not above or equal to 10000 ms. Please try again.");
success = false;
}
}catch(Exception e){
javax.swing.JOptionPane.showMessageDialog(null,"The specified time was not set in numbers exclusively. Please try again.");
success = false;
}
if(success){
gui.hideWorlds();
}
}
Note: it also checks whether a text box has a number input equal to or above 10000 (this works).
The code for the main JFrame:
if(jCheckBox5.isSelected()){
checkWorld = true;
if(!worldsChosen.isEmpty()){
changeWorlds = true;
}else{
log("You've selected the option for automatic world switching,");
log("but all of your inputs weren't formatted correctly.");
errorsHappened = true;
}
}else{
errorsHappened = false;
}
if(errorsHappened == true){
log("One or multiple worlds weren't added due to incorrect formatting.");
log("Retry to make script automatically change worlds.");
}
Whenever I run the script with the check box selected and something formatted correctly in the text area
(like this):
1
2
3
4
5
etc.
It outputs all of the log messages (as if the check box had been selected but none of the inputs were formatted correctly).
I've tried to the best of my knowledge to fix this, but I just can't see how it messes up.
Any help appreciated :).
Mike Haye.
Read the api doc of getText(int, int): the second argument is not an offset. It's a length.
Side note 1: it should probably be easier to get all the text as a single string and split on newline chars, and the parse every string into an integer.
Side note 2: The test if (jTextArea1.equals("")) can't succeed. A JTextArea instance will never be equals to a String instance.
I didn't check the complete program, but this is wrong:
if(!jTextArea1.equals("") && !jTextArea1.equals(null)){
Did you forget to add the call of getText() ? The line as it is will always be evaluated to true, because the instance object of JTextArea is never equal to "" or null. The latter would imply that the jTextArea1 object was null itself. Which would give you a NPE when you call the equals method.
Do you reset the flag before checking the conditions? Consider the following case:
//suppose errorsHappened is true here
if(jCheckBox5.isSelected()){ //we get true here
checkWorld = true;
if(!worldsChosen.isEmpty()){ //not empty, so this branch is taken
changeWorlds = true;
}else{ //worldsChosen is not empty, so this would not be logged
log("You've selected the option for automatic world switching,");
log("but all of your inputs weren't formatted correctly.");
errorsHappened = true;
}
}else{ //checkbox is selected, so no reset to false here
errorsHappened = false;
}
//due to the checkbox being selected and worldsChosen not being empty,
//errorsHappend is still true (which is wrong)