I wrote a method to load the page navigation links. The method works, but when I added code to check the correct URL and tab title my test is not performed. Sometimes it happens that for loop fast clicks on the pages the side that does not get loaded, I do not know whether it is a problem but I can not check whether a page loaded with the correct url or tab title, or the problem is the code that I wrote for check the correct url or tab title.
This is my method:
public void showNavigationLinks(){
Actions action = new Actions(driver);
String[] submenus = {"Accessories", "iMacs", "iPads" , "iPhones" , "iPods" , "MacBook"};
for(int i=0;i<submenus.length;i++)
{
WebElement we = driver.findElement(By.xpath("//a[contains(.,'Product Category')]"));
wait(2000);
action.moveToElement(we).moveToElement(driver.findElement(By.xpath("//a[contains(.,'"+submenus[i]+"')]"))).click().build().perform();
wait(3000);
waitForElementToBeDisplayed(driver.findElement(By.xpath("//a[contains(.,'"+submenus[i]+"')]")) , 500);
Assert.assertTrue(driver.getCurrentUrl().toLowerCase().contains(submenus[i]));
Assert.assertTrue(driver.getTitle().contains(submenus[i]));
}
link_all_product.click();
}
This is my error:
java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52)
at PageObject.ProductPage.showNavigationLinks(ProductPage.java:627)
One of your asserts is returning false, so your current title or url doesn't contain submenus[i]
You're converting the URL to lowercase here (driver.getCurrentUrl().toLowerCase()), but you're comparing it to your submenus, which isn't lowercase. This is probably your problem. Here is the fix:
String expected = submenus[i].toLowerCase();
String actualUrl = driver.getCurrentUrl().toLowerCase();
Assert.assertTrue(actualUrl.contains(expected));
For debugging purposes, you can step through your code to see what's happening, and/or you can make your error more meaningful:
Assert.assertTrue("Expected: " + actualUrl + " to contain: " + expected,
actualUrl.contains(expected))
Related
We are getting url from JSON Response and which we open in in Chrome.The page loads , there is submit button which we click then it redirect to url as :-
https://www.google.com/AB1234
We need the need to retrieve only "AB1234" value from url.
tried following code to get value ="AB1234"
String url = driver.getCurrentUrl();
int index=url.lastIndexOf("/");
String result = url.substring(0,index);
but here getting initial part of url:https://www.google.com/
You need to call substring function with index +1 .
Try below code :
String url = driver.getCurrentUrl();
int index = url.lastIndexOf("/");
String result = url.substring(index + 1);
To parse a URI, it's likely a good idea to use a URI parser.
Given http://example.com/bar
String path = URI.create(driver.getCurrentUrl()).getPath();
will get you '/bar'.
Given http://example.com/bar/mumble the same code gets '/bar/mumble'. It's unclear from your question whether this is what you want. Nevertheless, you should at least start the parse as above.
Hello all the following codes works here it is.
String content = FileUtils.readFileToString(new File("C:\\PayrollSync\\prepayroll.txt"));
String [] Arrayclients = content.split("\n");
// begin for loop
for(String client : Arrayclients) {
PP_OBJ_CycleData.verifysearch(driver);
PP_OBJ_CycleData.searchbox(driver).clear();
PP_OBJ_CycleData.searchbox(driver).sendKeys(client);
PP_OBJ_CycleData.searchbox(driver).sendKeys(Keys.BACK_SPACE);
Thread.sleep(4000);
//WebElement dropdown = driver.findElement(By.xpath(".//*[#title="+client+"]"));
//dropdown.click();
//driver.findElement(By.xpath(".//*[(text(),"+client+"]")).click();
driver.findElement(By.xpath("//*[contains(text(),"+client+")]")).click();;
Thread.sleep(2000);
PP_OBJ_CycleData.practitioner(driver).click();
The Problem:
None for the driver.findElements are working such as:
//WebElement dropdown = driver.findElement(By.xpath(".//*[#title="+client+"]"));
//dropdown.click();
//driver.findElement(By.xpath(".//*[(text(),"+client+"]")).click();
driver.findElement(By.xpath("//*[contains(text(),"+client+")]")).click();;
Thread.sleep(2000);
My failure traces says cannot find element By.xpath("//*[contains(text(),"+client+")]")). However I am waiting for the element to be visible when i go to the next page. My wait is below.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(20, TimeUnit.SECONDS);
element = driver.findElement(By.id("toolbarQuickSearch"));
wait.until(ExpectedConditions.or(
ExpectedConditions.visibilityOf(element),
ExpectedConditions.elementToBeClickable(element)
));
The problem from what I can see if that after the variable that contains the string is entered into the search field and the string name is visible selenium cant find the string name to click on for example below:
I could put this in and it will click on it all the time:
driver.findElement(By.xpath("//*[text()='midrfrate']"]"));
However if i do something like this it cant find it:
driver.findElement(By.xpath(".//*[(text(),"+client+"]")).click();
driver.findElement(By.xpath("//[contains(text(),"+client+")]")).click();
Please help me I have tried the following with no success:
How to use variables in XPath?
Your xpath there:
driver.findElement(By.xpath("//*[contains(text(),"+client+")]")).click();:
Should contain apostrophes
Text in contains block should contain exact String (you have trailing whitespace in client variable 'midrfrate ').
Final xpath is:
driver.findElement(By.xpath("//*[contains(text(),'"+client.trim()+"')]")).click();
where trim() method removes leading and trailing whitespace from client variable.
The string ".//*[(text(), " + client + "]" is equals to ".//*[(text(), midrfrate]"
You need to put the variable in apostrophes
".//*[contains(text(), '" + client + "']"
am trying to click on this game to open it's page but every time gives me null pointer exception whatever locator am using still gives me same error also i tried to do a select from list as the link seems to be inside an "li" but didnt work also.
anyone could help me with the code to click this item ??
Targeted Page Url:
https://staging-kw.games.getmo.com:/game/43321031
Search(testCase);
WebElement ResultList = driver.findElement(By.xpath(testData.getParam("ResultList")));
log.info("list located ..");
List<WebElement> Results = ResultList.findElements(By.tagName(testData.getParam("ResultListItems")));
for (WebElement List : Results) {
String Link = List.getAttribute("href");
try {
if (Link.equals(null)) {
log.info("null");
}
if (Link.equals(testData.getParam("GameLink")) && !Link.equals("") && !Link.equals(null)) {
List.click();
}
} catch (NullPointerException countryIsNull) {
log.info("LinkIsNull");
}
}
//clickLink(GameLocator,driver);
}`
I got this code to work by just adding this line after the search method
driver.findElement(By.cssSelector("li.title")).click();
and how did i get it ? .. i used Selenium IDE to record the actions then converted the code to Java -TestNG to get the exact web element selector
I need to make sure that a certain website code contains a div like this one:
<div id="iframe-content"> some code here... </div>
I tried this way:
elementTry = doc.select("div#iframe-content");
and next I stored it into the String
String someText;
someText = elementTry.toString();
and tried to show the output via Toast, but it gives me "null". I am sure the website contains the div with this id, so
what can I do to fix it?
EDIT:
Toast code
Toast.makeText(getContext(), "Passed: "+ someText, Toast.LENGTH_LONG).show();
Try using this
elementTry = doc.select("div#iframe-content");
String someText;
someText = elementTry.text();
Hope this helps
I am trying to add a hyperlink in java file.
TestHyperlink.java
class TestHyperlink.java {
String url = "stackoverflow.com/questions/ask";
String someVariable = "testUrl";
Html entryLink = new Html("<a target=\"_blank\" href=url>someVariable</a>");
}
I am trying to use two string variables url and someVariable but I am not sure how to do it. My hyper link appears as 'someVariable' and on click leads to a broken page.
What I seek is a hyperlink which appears as testUrl and on click leads to a desired url page, stackoverflow.com/questions/ask in this case.
Thanks,
Sony
Java doesn't interpolate variables inside Strings. You need to change to new Html("<a target=\"_blank\" href=\"" + url + "\">" + someVariable + "</a>");