Selenium webdriver 3.0 + java Wait until string entered - selenium

I'm trying to test my Hotmail account using selenium Webdriver 3.0. I set my account to ask for two way authentication, which means Indeed to enter the last 4 digits of my mobile number and then send a message to me. Then I have to enter that code in order to open my email Account.
It worked good with me when I used implicit, waited for 60 seconds until i receive the code then enter it manually and so the test continues to my email page >> all works fine.
BUT my question is, is there any way to make the test wait until I enter the code rather than waiting for 60s?!
Is that acceptable to enter some things manually while using Selenium webdriver?

is that acceptable to enter some things manually while using selenium webdriver?
Yes, because this thing is made for stopping robot activity just like Captcha code entering the process. So, in this scenario, this is acceptable to enter text manually after reading the text from mail while using selenium.
You can do one thing more if you want to do it automatically, you should implement mail API to read the last mail from your provided account in the background and fetch necessary text from the last mail using some programming stuff and enter it into textbox using selenium.
is there any way to make the test wait until I enter the code rather than waiting for 60s?
Yes, to achieve this you need to create your own custom ExpectedConditions with WebDriverWait which will wait until located text box has value greater or equal 4 character or other suitable condition which you want as below :-
//Initialize WebDriverWait first which will wait maximum 60 seconds
WebDriverWait wait = new WebDriverWait(driver, 60);
//Create suitable locator to locate textbox element eg. with xpath locator
By byObject = By.xpath("enter here textbox xpath");
//This condition will wait until text box has value greater or equal 4 character
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return (d.findElement(byObject).getAttribute ("value").length() >= 4)
}
});

Related

Selenium command taking more time than specified in wait

I have a selenium test to navigate to a login page. Enter user name and password and click the login button. Once logged in, check whether an element is present in the home page.
I am keeping track of the time each command takes
WebDriver driver = driver.manage().timeouts().pageLoadTimeout(30L, TimeUnit.SECONDS);
driver.get("<url>");
WebDriverWait webDriverWait = new WebDriverWait(driver, 20);
Even though my wait is for 20 seconds, When I check the total time for this command to execute it can take up to 30 (this happens on failed logins). I am trying to figure out why selenium is taking 30 seconds instead of 20 in case of failures? I read through the documentation etc but no where did I see that pageLoadTimeout impacts loading when a button is clicked.
It seems like the driver.get("<url>"); command takes some time.
Means the page needs some time to load. You could load the page threaded tho.

How to check wether am in correct page after submitting the form using assertion selenium/TestNG/Java

This is my scenario "i am submitting the form and once I submit its navigate into page call WebTable.I want to confirm now am incorrect page after submitting the form"
how to check this by using assertion? please help selenium /Java /TestNG
In my experience, you have basically two options available to you.
Wait for the URL to contain what you expect using driver.getCurrentUrl()
Wait for a specific element that will only appear on the next page
If you don't have a class already built for performing retries, you can use FluentWait.
Once you click the form you wait for an element to show up using Webdriverwait.
You pull the current URL and check with value and assert it.
submitBtn.click();
WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.id("etc")));
expectedUrl = driver.getCurrentUrl();
Assert.assertTrue(expectedUrl.equals("something"));
Selenium provides a good practice for this type of validation, and here comes FluentWait:
FluentWait fluentWait = new FluentWait(webDriverBPO).withTimeout(Duration.ofSeconds(20)).pollingEvery(Duration.ofSeconds(1)).ignoring(NullPointerException.class);
All you have to do after your last action, is to wait until URL contains your expected value:
fluentWait.until(ExpectedConditions.urlContains("your URL or keyword from URL"));
You can try also to wait for any element from the new page, but the URL loads faster than the content, so you will save some time (as you don't need to perform any other validations).

Selenium Send keys intermittently skips over text fields

I am writing automation tests that send keys to every text box on a form (about 5 fields) and then a submit button becomes enabled and clicked. I find that the test frequently fails as only the last field is populated and the button never becomes enabled.
It seems like the method to send keys may be executing too fast for the page to be populated. So far I have attempted to click on each field before sending keys (as well as waiting for the elements to exist) and this doesn't seem to help.
I have also tried to verify the page is fully loaded by waiting for every element and button to exist on the page before proceeding.
The browser I am testing against is chrome. (Version 79.0.3945.130)
The selenium web-driver is 3.11.2
The chrome driver is up to date with chrome
private IWebElement FirstNameInput => Webdriver.FindElement(By.Id("first-name-input"));
// The remaining input fields
public void VerifyPageIsFullyLoaded()
{
// Wait until all elements exist
}
public void EnterFormDetails(FormDetail formDetail)
{
WebDriver.WaitUntilElementExists(FirstNameInput);
FirstNameInput.Click();
FirstNameInput.SendKeys(formDetail.FirstName);
WebDriver.WaitUntilElementExists(LastNameInput);
LastNameInput.Click();
LastNameInput.SendKeys(formDetail.LastName);
WebDriver.WaitUntilElementExists(DateOfBirthInput);
DateOfBirthInput.Click();
DateOfBirthInput.SendKeys(formDetail.DateOfBirth);
WebDriver.WaitUntilElementIsClickable(SubmitButton);
SubmitButton.Click();
}
Update:
Just tried the latest stable release of selenium web driver (3.141.0) and found that it is still not as reliable.
JavascriptExecutor javascript = (JavascriptExecutor)webdriver;
javascript.executeScript("document.getElementById('FirstNameInput').value='test123'");
javascript.executeScript("document.getElementById('LastNameInput').value='test123'");
javascript.executeScript("document.getElementById('DateOfBirthInput').value='test123'");
SubmitButton.Click();
and if the submit button is in form tag then you can use submit() method.

Selenium Webdriver - how to find css/xpath for 'inline warning message' that disappears after few seconds?

I was trying to capture or (find the css/xpath) the inline warning message that disappears after few seconds (BTW, I am using Selenium WebDriver / Java for my automation).
eg: In the below public link, I try to click Reset Button without entering any email. The text box briefly shows 'Please fill out this field." I want to automate if it is showing this message as expected.
https://app.shipt.com/password_resets/new
Please help.
PS: I tried to search this website and google but could not find any useful information.
For actions that appear or disappear after certain time you should use Expected Conditions:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
And then you can click on element as usual.
However in case of the shipit page you are trying to automate the popup is a native HTML5 popup, so you cannot use Selenium directly to get the message, and you have to use this workaround:
Stackoverflow - how to get HTML5 error message in Selenium

how to pause selenium web driver for user action

I want to automate an application where user has to enter the OTP at a particular interval . Is it possible to automate is such a way using Selenium?
Ideally there should be no Manual Intervention when Automated Test Execution is In Pogress.
If entering an OTP is one of the step of the Usecase it should be automated too.
Note : OTP can be sent to an email address and can be validated.
If your are running the tests in Dev environment, One possible solution would be to request the developers to set a constant value for the OTP and then validate the field with the value.
More complex solution would be to get the value from message box which will include appium concepts or to read the OTP value from the mail box
You don't have to handle this with Selenium, rely on programming language. Display a box/message, when user enters pass that to the form via selenium send keys and continue your automation. Selenium will actually wait unless your page expires. Below code can help.
driver.findElement(By.id("username")).sendKeys("username");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String otp;
System.out.println("Please Enter the OTP Code:: ");
otp = br.readLine();
System.out.println("Entered OTP Code is:: "+otp);
driver.findElement(By.id("password")).sendKeys("otp");
Suppose you want to enter your OTP at this page,
www.your-page.com/OTP
once this page spawns, use new Scanner(System.in).nextLine
to manually wait for user-input of any key on your console. Before pressing Enter on your console, you can insert your OTP in the page (and press submit if any) after which you can resume your execution from your console by pressing any key.
This shall create a manual-halt-like feature during which you can perform your manual interactions.