how to handle Mobile OTP using selenium - java

There is one case that, when i open the URL manually in chrome browser and give the credentials.
After clicking on sign in button one OTP will come to mobile. There is one checkbox after entering the OTP. Don't ask OTP for this browser. If we click on that check box and login into the portal. Then from next time if we open same browser and access the portal, it will not ask for OTP. So can we automatically open same browser every time after closing browser. Without calling new chrome instance.

You can use the default profile to use the local chrome profile once the OTP is completed manually.
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");
ChromeDriver driver = new ChromeDriver(options);
You can get the default profile path as shown in the below screenshot.
Please open chrome://version in the browser to see what profile Chrome is using.

Related

Unable to capture text inside new window in Java Selenium Headless browser

I'm running all my test suites on Jenkins which is deployed on AWS EC2 instance. There is a scenario where when I click on a button, new small window opens up and I'm doing assertion for the text visible inside the small newly opened window. But my tests are failing when I run using Headless mode. But, same scripts works fine when I run scripts locally without opting for headless browser.
The issue here is the scripts are failing because of headless browser since it's unable to capture text inside small window which has opened after click of button.
This class is extending InitiateDriver class which explained. Below class is trying to fetch text which is visible inside the new window which just opened after clicked on SignInWithGSuiteSSOClick() button.
Here is the code:
// click on a button
GSuiteobject.SignInWithGSuiteSSOClick().click();
String winHandleBefore = driver.getWindowHandle();
// Here trying to capture text inside new window opened up basically gmail window to enter email
String signInHeader = GSuiteobject.GsuiteSignInHeader().getText();
Assert.assertEquals(signInHeader, "Sign in");
GSuiteobject.GsuiteEmail().sendKeys("example#gmail.com");
InitiateDriver.java
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--window-size=1920, 1080");
options.addArguments("--disable-gpu");
driver = new ChromeDriver(options);
The same code works in browser mode but not in headless. But the driver will be initialized but it fails only while capturing text. Please help me out I'm stuck here and unable to execute it on Jenkins as a headless browser.
Headless chrome browser's gmail UI will be different from actual gmail UI(latest). Hence it fails when we run headless chrome for automating gmail login since xpaths will differ. We can validate that by taking Screenshot by running on headless and normal browser.
I suggest to take screenshot using both headless and normal browser. To check on xpaths for headless we can try driver.getPageSource() method.

Log in to facebook using selenium in Java

I try to log in to facebook using selenium library but I occur some problems. After it inputs the credentials and click the login button I get an error which says:
Cookies Required
Cookies are not enabled on your browser. Please enable cookies in your browser preferences to continue.
I've looked over the internet for an answer so I've tried with driver.manage().getCookies(); and I get the same result.
Here's my code:
System.setProperty("webdriver.chrome.driver", DRIVERLOCATION);
WebDriver driver = new ChromeDriver();
driver.manage().getCookies();
driver.get("https://www.facebook.com");
driver.findElement(By.id("email")).sendKeys(USERNAME);
driver.findElement(By.id("pass")).sendKeys(PASSWORD);
driver.findElement(By.xpath("//input[starts-with(#id, 'u_0_')][#value='Log In']")).click();
I know in the question I said that I try to reach www.facebook.com but in the real project I was reaching a profile.
It works just fine when you go to https://www.facebook.com but the error occurs when your going to a profile or if you have second authentication activated.

Firefox selenium webdriver gives "Insecure Connection"

I've created a Maven project with 20 tests made with Selenium Webdriver (java). Now, when I want to execute my Maven project, I get sometimes the following error:
Mozilla error
This is due to login every test. So, when I want to run 20 tests, sometimes that error appears and I can't continue my test, so it returns "Failed test" in Selenium Webdriver.
Does anybody know how to fix this problem?
I've tried to put "Thread.sleep(30000);" at the end of every test to give them some time "not to seem a robot", but it doesn't work...
Thanks so much for your help guys!
Here is the Answer to your Question:
The Real Issue:
The URL/Connection with which you are a working if it is Not Secure then whenever you access the URL through Mozilla Firefox 53.0, Firefox will display a lock icon with red strike-through red strikethrough icon in the address bar. Now when URL gets loaded the cursor by default will be positioned on Username field and there will be popup showing a message This connection is not secure. Logins entered here could be compromised. Learn More like this:
Now your script through Selenium enters the username within the Username input field and the Not Secure popup overlays the Password input field.
Next if you try to call the click() or sendKeys() operation in the Password input field the Not Secure popup receives the click and Insecure password warning in Firefox page opens up in the next tab along with Selenium shifting its focus to new tab. Hence test-case starts Failing.
Solution:
In these cases the best solution is:
Create a new Mozilla Firefox Profile. You will find the documentation here. For Example, I have created a Firefox Profile by the name debanjan
Configure the Firefox Profile debanjan to ignore all the UntrustedCertificate issues.
Rerun your Test Script without any issues.
Here is a sample code block to disable insecure_field_warning:
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
testprofile.setAcceptUntrustedCertificates(true);
testprofile.setAssumeUntrustedCertificateIssuer(true);
testprofile.setPreference("security.insecure_field_warning.contextual.enabled", false);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, testprofile);
dc.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(dc);
driver.manage().window().maximize();
driver.navigate().to("http://demosite.center/wordpress/wp-login.php");
Let me know if this Answers your Question.

Remember credentials with serenity BDD (using selenium)

Im trying to create test cases with Serenity BDD (using selenium).
I have accepted the fact that between each test case it "restarts" the browser.
And I found a way to not do between Scenarios.
But what Im looking for is to run the test in normal browser mode and not in incognito.
I want the browser to click the "remember me" option, log in, log out, and see username and password in the fields.
But since the tests are running in the browser with incognito mode, the password is blank.
Any suggestions ?
Thank you
You can configure your driver to run on specific profile of browser.
Below is the Mozilla link with steps to configure your profile:
https://support.mozilla.org/en-US/kb/profiles-where-firefox-stores-user-data
Once you have configured that profile you can launch that browser manually and save all the user name and passwords manually.
So next time when you launch this profile of browser user name and password fields will be auto filled.
*ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile();
WebDriver driver = new FirefoxDriver(myprofile);*

Application throws warning message to enable cookies while running through IEDriver with selenium webdriver

I was automating an angularjs application using selenium webdriver. I am using IEDriver 32 bit version to driver the IE execution. Typically, the scenario is admin user will register a user, this user will click on the registration link mailed to their maid id. Then it will input some required information, automatically it will be navigated to login page where user have to input the username and password and login.
But application warns to enable cookies at this point of time. If we are renavigating to the login page there is no issue.
This issue is not reproducible everytime.
Is there any way to enable cookies while setting up the IE profile?
There are many ways as you can find cookies in the below path
In Windows 8 for IE10, cookies are saved in the hidden protected OS folders below:
C:\Users(user name)\AppData\Roaming\Microsoft\Windows\Cookies
you can open IE and then navigate to internet options/privacy/advanced privacy settings.
There you can accept and block cookies.

Categories

Resources