Environment
Azure websites
Dot.net framework
Configuration
Selenium 3.14
Google Version 96.0.4664.110
Challenge
Using selenium automate Google 2 step mobile verification while singing in to application.
Tried using below code but not working.
import org.jboss.aerogear.security.otp.Totp;
public class TOTPGenerator {
/**
* Method is used to get the TOTP based on the security token
* #return
*/
public static String getTwoFactorCode(){
//Replace with your security key copied from step 12
Totp totp = new Totp("nlyyriaxspwdomi7buvo32cuas6tz7aa"); // 2FA secret key
String twoFactorCode = totp.now(); //Generated 2FA code here
return twoFactorCode;
}
}
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TwoFactorGmail {
public static void gmailSignIn(){
System.setProperty("webdriver.chrome.driver", "path of the exe file\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("your application url which embeds gmail sign-in");
driver.findElement(By.id("identifierId")).sendKeys("abc#gmail.com");
driver.findElement(By.id("identifierNext")).click();
driver.findElement(By.name("password")).sendKeys("password");
driver.findElement(By.id("passwordNext")).click();
// OTP value is returned from getTwoFactor method
driver.findElement(By.id("totpPin")).sendKeys(TOTPGenerator.getTwoFactorCode());
driver.findElement(By.id("totpNext")).click();
}
}
Related
I am new to automation testing and I am currently trying to automate a login form, user name tomsmith and pass supersecret password
However when i run my below script i only get to the login page, I am unsure why it is not inputing the details. I also would like to logout automatically as well.
package Form;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Authentication {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//http://tomsmith:SuperSecretPassword!
driver.get("http://tomsmith;SuperSecretPassword!#the-internet.herokuapp.com/login");
String pageMessage = driver.findElement(By.cssSelector("p")).getText();
System.out.println(pageMessage);
}
}
Pass your values to the input tags.
driver.findElement(By.ID('username')).sendKeys("tomsmith");
driver.findElement(By.ID('password')).sendKeys("SuperSecretPassword!");
I have to use existing user login session, we will require EDGE user profile, as we observed EDGE driver doesn't use the existing user data profile it is creating a new profile every time
EDGE Default Profile Path
C:\Users\edge2automation\AppData\Local\Microsoft\Edge\User Data\Default
(Edge driver) path -
C:\Users\edge2automation\AppData\Local\Temp\scoped_dir64860_1277252271\Default
As the new Edge is based on Chromium, we can refer to the solution of using Chrome profile and change the key words to Edge:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
public class Edgeauto {
public static void main(String[] args) {
System.setProperty("webdriver.edge.driver", "your\\path\\to\\edge\\webdriver\\msedgedriver.exe");
EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.addArguments("user-data-dir=C:\\Users\\edge2automation\\AppData\\Local\\Microsoft\\Edge\\User Data");
edgeOptions.addArguments("--start-maximized");
WebDriver driver = new EdgeDriver(edgeOptions);
driver.get("https://www.google.com/");
}
}
Please note that this needs to use selenium-server-4.0.0-alpha-4 which you can download form here.
Hello I am new to automation and i have tried automating dropbox.com sign in but my code is failing after clicking on sign in link. i am not able to pass the values (username and password) in the frame of the sign in box. Following is my code.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Drop_box {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get("http://www.dropbox.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[#id='sign-in']")).click();
driver.findElement(By.xpath("//*[#id='pyxl1851']")).sendKeys("123#gmail.com");
driver.findElement(By.xpath("//*[#id='pyxl1854']")).sendKeys("1234");
driver.findElement(By.xpath("//*[#id='regular-login-forms']/form[1]/div[3]/button")).click();
}
}
This should work:
driver.findElement(By.cssSelector("a#sign-in")).click();
driver.findElement(By.cssSelector("div#index-sign-in-modal input[name='login_email']")).sendKeys("email");
driver.findElement(By.cssSelector("div#index-sign-in-modal input[name='login_password']")).sendKeys("password");
driver.findElement(By.cssSelector("div#index-sign-in-modal div.sign-in-text")).click();
If you prefer xpath over css selectors, then use following lines of code:
driver.findElement(By.xpath("//*[#id='sign-in']")).click();
driver.findElement(By.xpath("//div[#id='index-sign-in-modal']//input[#name='login_email']")).sendKeys("email");
driver.findElement(By.xpath("//div[#id='index-sign-in-modal']//input[#name='login_password']")).sendKeys("password");
driver.findElement(By.xpath("//div[#id='index-sign-in-modal']//div[#class='sign-in-text']")).click();
Hi I am learning selenium grid for the first time.I am unable to understand why Desired capabilities class is used to set browser name and platform details in a remote machine.
please let me know if there is any proper documentation on it apart from google-wiki page
Thanks
prathima
Not all remote machines (or sessions) support the features requested by a user. E.g. user may want to use Chrome on Windows but some machines are only running Firefox on Linux.
The desiredCapabilities is used to describe the features of a session requested by user. You can refer to this link and this link for more info.
- when user send request to remote machine to run a particular test on
a particular browser on a particular platform.
- It doesn't mean that all the remote machine will support all the
features requested by user such as (Particular browser on a
particular machine).
**Example:**
- If user request to execute a testcase on internet explorer on Linux
platform. It doesn't mean that always Linux machine has internet
explorer it would have firefox not internet Explorer(Because it is
open source).
- Linux machine doesn't support the features requested by a User. so
that, that point of time we will use DesiredCapabilites class to set
the platform , Browser Name and version of browser of Remote Machine
to execute the Test.
Example:
package grid;
import static org.junit.Assert.*;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class SimpleGridTest
{
WebDriver driver;
String baseUrl , nodeUrl;
#Before
public void setUp() throws Exception
{
WebDriver driver;
String baseUrl , nodeUrl;
baseUrl = "https://www.facebook.com";
nodeUrl = "http://192.168.10.21:5568/wd/hub";
DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setBrowserName("firefox");
capability.setPlatform(Platform.WIN8_1);
driver = new RemoteWebDriver(new URL(nodeUrl),capability);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.MINUTES);
}
#Test
public void test() throws InterruptedException
{
driver.manage().window().maximize();
driver.get("https://www.google.co.in");
driver.findElement(By.linkText("Gmail")).click();
driver.findElement(By.id("Email")).sendKeys("abc#gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("1234");
driver.findElement(By.id("signIn")).click();
}
#After
public void tearDown() throws Exception
{
driver.quit();
}
}
I use Selenium Web Driver with JUnit in Eclipse. I want diminish my code by creating new classes for repitable steps. I want store this classes in separate files for convenience. For example this one of such classes:
import org.openqa.selenium.ie.InternetExplorerDriver;
//Login
public class Login {
private InternetExplorerDriver driver;
String url;
String name;
String password;
String language;
public Login (String ur, String nam, String pass, String lang){
url=ur;
name=nam;
password=pass;
language=lang;
}
public void log_in (){
driver.get(url);
driver.findElement(By.name("username")).sendKeys(name);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.name("language")).sendKeys(language);
driver.findElement(By.name("logon_action")).click();
}
}
This is my main test:
package bobsworld;
import init.Login;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class WebTest {
private WebDriver driver;
#Test
public void testUnit() throws Exception {
//Open new window
System.setProperty("webdriver.ie.driver", "IEDriverServer.exe");
driver = new InternetExplorerDriver();
//Login
init.Login login = new init.Login ("myurl", "log","pas","English");
login.log_in();
}
The problem is with object driver. I get java.lang.NullPointerException and can't execute test. How should I organize call to Login and code to make my test work?
What you try to achieve is a quite popular approach called "PageObjects".
Your issues:
Use some checkstyle tool to improve code quality
Do a whole lot of research about Java in general
The Login Class or "init.Login" (it's quite unusual to have a part of the package as prefix for the class) is not able to use the same WebDriver instance because you don't forward it. Normally you would have opened a second WebDriver instance but that seems to be impossible though I don't know your setup. You have to forward the "driver" as a parameter to the constructor of the Login class.
Try to use real PageObjects as described here inluding PageFactory to improve readability and maintainability