selenium POM framework and TestNG wait Time [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
In my Test script have to write waiFor(5) for every excution, else Test fails.
Am using POM, and have separate class.
#Test(priority=2)
public void TestMedical() throws InterruptedException {
waitFor(5);
MedicalPage medicalpage = new MedicalPage(driver);
waitFor(5);
medicalpage.PhysicianName(Repo.getProperty("fName"));
medicalpage.seteditableLastName(Repo.getProperty("lName"));
waitFor(5);
medicalpage.setPhone(Repo.getProperty("Phone"));
waitFor(5);
medicalpage.setEmail(Repo.getProperty("Email"));
}
This is my Login Page (Object)-
// Created methods in this page, and using Testng trying to call all the below methods. But getting failure message Unable to Locate Element, If there is no wait time watFor(5). With Wait time its running fine.In base page have explicit wait time method, but its not working. After input the data in text field when i click on submit button page is doing some Java Script or Ajax.
Ajax call takes 60 sec max.
driver.findElement(firstName).click();
driver.findElement(editableFirstName).clear();
driver.findElement(editableFirstName).click();
driver.findElement(editableFirstName).sendKeys(fName);
}

Added try - catch, works fine now
WebElement element = driver.findElement(XYZ);
boolean clicked = false;
do{
try {
element.click();
} catch (WebDriverException e) {
continue;
} finally {
clicked = true;
}
} while (!clicked);;

Related

Java response get doubled on page refresh [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 days ago.
Improve this question
My API response gets doubled every time I refresh my page. Is it because my return object is declared outside and every time I refresh it creates a new entry?
#GetMapping(value = "/getPaymentMethodDetails")
public List<AutoPayAccountBean> executePaymentMethodDetails(HttpServletRequest request) {
executeGetPaymentMethodsForUser(request);
if (Objects.nonNull(getPaymentMethodsList())) {
getPaymentMethodsList().stream().forEach(paymentMethod -> {
AutoPayAccountBean accountBean = new AutoPayAccountBean();
accountBean.setPaymentMethod(paymentMethod.getProfileName());
accountBean.setExpirationDate(expirationDate);
accountBean.setHolder(paymentMethod.getHolder());
accountBean.setStatus(paymentMethod.getStatus());
this.paymentMethodResponse.add(accountBean);
});
}
return paymentMethodResponse;
}

Assert the URL or Navigate to it appropriately [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have one question - how to write a cucumber hook that asserts the web URL.
Example -
Let's say I am testing - https://www.usatoday.com/tech/
My Given is as follows -
Given I am on the "/tech/" page
I would like a method to add the base url (www.usatoday.com) + "/tech" and assert whether it is currently on that page
(driver.getURL() ==www.usatoday/tech/)
otherwise navigate to that page if I am not there.
Any Java pros that can help me with this? Many thanks my friends.
#Given("^I am on the \"([^\"]*)\" page$")
public void i_am_on_the_page(String arg) throws throwable{
String newBaseURL = getBaseURL() + arg;
String currentURL = driver.getCurrentURL();
try{
Assert.assertEquals(newBaseURL, currentURL);
}
catch(Exception e){
driver.get(newBaseURL);
}
}
I am assuming that you have a function - getBaseURL() that will provide you the base url.

Using IF commands to Check if a button exists in Selenium Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a java selenium program that automatically presses on a button in a website. The code i am using looks like this :
driver.findElement(By.id("button")).click();
I want the program to use if statements if possible.
Thanks for your help.
Use can use findelements by along with if to achieve this. Below code might give you some idea.
if(driver.findElements(by.xpath("//*[#id=253]")).size>0)
{
//element exists with id = 253
// do the stuff
} else
{
//element do not exist with id = 253.
// do the stuff
}
Hope this helps. Thanks.
You may try this:
public void ClickButton () throws InterruptedException
{
WebElement button = driver.findElement(By.id("button"));
String Source = driver.getPageSource();
if (Source.contains(button))
{
button.click();
Thread.sleep(3000);
}
else
{
driver.quit;
}
}
Hope this can be helpful. Let me know if you are still facing problem.

Ensuring if excel worksheet contains any user defined function [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
My application contains an upload option from which the user can upload files(any file).
I know that users have the ability to write code/scripts in excel worksheet.So how can i prevent that from happening as it can lead to security breach.
Or what check can i apply to know that the uploaded worksheet contains any function?
I assume from the Java tag that your appliction to which the sheet gets uploaded is a Java application.
Using Apache POI, you can use a POIFSReader (see this doc) to read the file on a lower level.
There is an example by RĂ©al Gagnon which shows how you can check the content by looking at the name.
I had to modify the listener (added another comparision)
class MacroListener implements POIFSReaderListener {
[...]
#Override
public void processPOIFSReaderEvent(POIFSReaderEvent event) {
System.out.println(" Event: Name " + event.getName() + ", path "
+ event.getPath());
if (event.getPath().toString().startsWith("\\Macros")
|| event.getPath().toString().startsWith("\\_VBA")
|| event.getPath().toString().contains("_VBA_")) {
this.macroDetected = true;
}
}
}
so that it worked for some test files. You might have to tune/extend the checks.

How to find if a text exists on page using selenium [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
If the text exists then click on xyz else click on abc.
I'm using the below if statement:
if(driver.findElement(By.xpath("/html/body/div[2]/div/div/div/div/div/div/table/tbody/tr[6]/td[2]")).isDisplayed())
{
driver.findElement(By.linkText("logout")).getAttribute("href");
} else {
driver.findElement(By.xpath("/html/body/div/div/div/a[2]")).click();
}
Script fails with the following error message:
Unable to locate element: {"method":"xpath","selector":"/html/body/div[2]/div/div/div/div/div/div/table/tbody/tr[6]/td[2]"}
Try this code:
The below code used to check the text presence in the entire web page.
if(driver.getPageSource().contains("your Text"))
{
//Click xyz
}
else
{
//Click abc
}
If you want to check the text on a particular web element
if(driver.findElement(By.id("Locator ID")).getText().equalsIgnoreCase("Yor Text"))
{
//Click xyz
}
else
{
//Click abc
}
First of all, XPaths of this type and byLinkText are really bad locators and will fail frequently. The locators should be descriptive, unique, and unlikely to change. Priority is to use:
ID
Class
CSS (better performence than XPath)
XPath
Then you can use getText() on the element rather than on the entire page (getPageSource()) which is more specific. try catch is also a good practice for isDisplayed() as #Robbie stated, or better yet use FluentWait to find the element:
// Waiting 10 seconds for an element to be present on the page, checking
// for its presence once every 1 second.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(10, SECONDS)
.pollingEvery(1, SECONDS)
.ignoring(StaleElementReferenceException.class)
.ignoring(NoSuchElementException.class)
.ignoring(ElementNotVisibleException.class)
Then use like so:
wait.until(x -> {
WebElement webElement = driverServices.getDriver().findElement(By.id("someId"));
return webElement.getText();
});
wait.until(x -> {
WebElement webElement = driverServices.getDriver().findElement(By.id("someOtherId"));
return webElement.getAttribute("href");
});
Here we can use try ,except function using python web driver. see below the code
webtable=driver.find_element_by_xpath("xpath value")
print webtable.text
try:
xyz=driver.find_element_by_xpath(("xpath value")
xyz.click()
except:
abc=driver.find_element_by_xpath(("xpath value")
abc.click()
You need to wrap the "IsDisplayed" in a try catch. "IsDisplayed" can only be called if the element exists.
You may want to override the Implicit Time Out as well, else the try/catch will take a long time.
Try this below code:-
Assert.assertTrue(driver.getPageSource().contains(textOnThePage));

Categories