Selenium Java-Object changes location dynamically - java

There are 3 categories of users: Authorized, Non-Authorized and Total Users. Users can be created through UI and by default the users will be displayed in the Non-Authorized section. There is a Toggle button available for users on each of the 3 page sections. When user clicks on the toggle button for a user from Non-Authorized section the user entry goes in the Authorized page.
Automation Requirement is to click on every toggle button of the user, check if all the buttons are clicked and then navigate to page users to check if all users are added.
The problem is that the when the toggle button is clicked of any user the top/bottom record HTML position gets replaced with the preceeding one.
eg: if the path of the 1st toggle button is //[#id='xyz']/td1 and user clicks on this toggle button the next user records toggle buttons path now becomes same as above i.e//[#id='xyz']/td1
Below is the html code of the toggle switch:
<span class="bootstrap-switch-handle-off bootstrap-switch-default" style="width: 20px;"></span>
I have tried to add Thread.sleep() after every toggle button click so that there is a delay and then my code can click on the next toggle switch using the same path but was wondering if there is a optimized way to handle this scenario.
Any Suggestions?

Try to click first toggle and wait until it disappears inside while loop with any toggle exists condition:
WebDriverWait wait = new WebDriverWait(driver, 10);
while (driver.findElements(By.cssSelector(".bootstrap-switch-handle-off")).size() > 0) {
WebElement toggle =
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".bootstrap-switch-handle-off")));
toggle.click();
//wait.until(ExpectedConditions.invisibilityOf(toggle));
wait.until(d -> {
try {
return !toggle.isDisplayed();
} catch (StaleElementReferenceException ignored) {
return true;
}
});
}

Related

Continue button is clicked but does not navigate to the NEXT step on the same page in selenium java

I'm trying to automate a site but stuck at the point where I need to complete the onboarding process of the user account.
So the problem is I have to add a user and to add the user I have to go through few steps, I have successfully added the user details but when I click on continue button it does not navigates me to the next on same page.
I want to know that how can I navigate to the next step on the same page button by clicking the continue button
Here is my code
public void enter_advisor_details() {
driver.findElement(user_mgmt_opt).click();
driver.findElement(advisor_tab).click();
driver.findElement(add_advisor_btn).click();
driver.findElement(first_name).sendKeys("Test");
driver.findElement(last_name).sendKeys("Automation");
driver.findElement(email).sendKeys("TestAuto#gmail.com");
WebElement element = driver.findElement(By.xpath("//div[#class='mt-8']//button"));
element.click();
}
Note: I have tried Actions class, WebDriverWait and JavaScript executor but nothing works for me
As you can see in the below image the test is getting passed and the button is clicked and the next step does not show up
enter image description here
You are probably missing a delay. Wait for element to become clickable before clicking it, as following:
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='mt-8']//button"))).click();

Using Action Class, After ClickAndHold, how can I go to two step bottom menu option and click?

{ actions.clickAndHold(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.DOWN)
.sendKeys(Keys.ENTER).perform(); }
enter image description here
Can't you use something like this? Obviously the selectors I used can be improved. I am not sure why we are clicking and holding and then pressing keys down. When we do this manually, that is not the action that we are taking. We basically move to the Rankings menu, then move to the womens link, then click if needed.
driver.get("https://www.cricbuzz.com");
WebElement rankingMenu = driver.findElement(By.xpath("//a[contains(text(),'Rankings')]"));
WebElement womensSelect = driver.findElement(By.xpath("//a[#title='ICC Rankings Women']"));
Actions action = new Actions(driver);
action.moveToElement(rankingMenu).moveToElement(womensSelect).click().build().perform();

Checkbox click with Firefox using selenium 3 not working?

I have below code to uncheck check box when it is selected, but when i run test i could see checkbox is unchecked for less than few seconds and again it is auto checked after less than few seconds. But I have no issues with Chrome and IE.
I am using Firefox Quantum(58.0.2 64-bit) and rememberMe is checkbox id.
if (driver.findElement(By.xpath("//*[#id='rememberMe']")).isSelected()) {
Thread.sleep(6000);
driver.findElement(By.xpath("//*[#id='rememberMe']")).click(); --> here it should uncheck check box.
Thread.sleep(6000);
}
I have used all other options like wait on element, send keys etc. Could some one please point me in right direction?
I would do something more like this
WebElement checkbox = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("rememberMe")));
if (checkbox.isSelected())
{
checkbox.click();
}
It waits for the element to be clickable then clicks it if it's selected.

Methods used to verify a "cancel" button in web application works

I am working on test cases for a web application. The current job requires me to confirm that when you press the logout button, the prompt comes up, you can click the cancel button on the prompt, and the prompt will close.
I am wondering about verify and assertion methods I could use to confirm this functionality works. The cancel button does nothing else but close the pop up prompt. What would you guys use?
Here's some code:
Actions actions = new Actions(driver);
WebElement logout = driver.findElement(By.xpath(".//*[#id='flow']/div[1]/div/div[7]/div/div[3]/div[4]/div/div/div/div/div/div/div/div[1]/div/img"));
actions.moveToElement(logout).build().perform();
WebElement logoutHover = driver.findElement(By.xpath(".//*[#id='flow']/div[1]/div/div[7]/div/div[3]/div[4]/div/div/div/div/div/div/div/div[3]/div/img"));
logoutHover.click();
WebElement logoutPushed = driver.findElement(By.xpath(".//*[#id='flow']/div[1]/div/div[7]/div/div[3]/div[4]/div/div/div/div/div/div/div/div[4]/div/img"));
logoutPushed.click();
WebElement cancel = driver.findElement(By.xpath("html/body/div[3]/div[4]/div/div/div[6]/div[2]/div/div[2]/div/div[3]/div/div/div/div[5]/div"));
actions.moveToElement(cancel).build().perform();
WebElement cancel2 = driver.findElement(By.xpath("html/body/div[3]/div[4]/div/div/div[6]/div[2]/div/div[2]/div/div[3]/div/div/div/div[5]/div"));
cancel2.click();
WebElement pageText = driver.findElement(By.xpath("html/body/div[3]/div[1]/div/div[3]/div/div/div/div/div/div/div/div/div[2]/div[2]/div/div/div/div/div[2]/div"));
Assert.assertTrue("Text not found!", pageText.contains("PRODUCT LIST"));
This assert method does not work. My initial idea was that if you hit the cancel button, I can assert that the user is still on the same page (my code for this does not work). Would it be a smarter choice to assert that the prompt is not present anymore? If so, how would I go about doing that?
I believe the correct way to do it would be to confirm that the logout prompt is no longer up and that you are still logged in. I don't know what your site looks like but for the logout prompt, find an element unique to that popup (maybe the OK or cancel button, hopefully something with an ID). Detect that it's no longer visible and then confirm you are still logged in by some means... look for a user name or ???
// click the logout button
// click the cancel button
List<WebElement> button = driver.findElements(By.id("sampleId")); // a button on the confirm popup
if (button.isEmpty())
{
// the logout confirmation popup is not visible
// verify that you are still logged in... maybe look for a user name or ???
}
else
{
// log a failure here because you couldn't cancel the logout popup
}

Click() is not performing on Fb message(left panel) and Home button

I tried different xpath (relative and absolute) and perform .click() on them in Facebook message (left panel) and Home button on upper nav bar but the element is highlighted (like when you hover the mouse pointer on it) but not clicking on them.
My code snippet is like this:
d.findElement(By.xpath("//div[#class='_59g8']/div[#class='clearfix']/div[1]/ul[1]/li[2]")).click();
Thread.sleep(1000L);
d.findElement(By.xpath("//div[#id='pinnedNav']/ul[1]/li[2]")).click();
After login fb page. You can try with the below lines of code, it will perform click action on Home and Message Button.
d.findElement(By.linkText("Home")).click();
Thread.sleep(1000);
d.findElement(By.partialLinkText("Messages")).click();

Categories

Resources