Skip to content
Advertisement

Automating a login with out providing the password via selenium Webdriver

I am trying to create an automated test to one of my accounts. I have manage to do so but i have to provide the password via driver.sendKeys(). Any idea on how i could automate this part without providing my password? I have attached the code below

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class LoginToTradeville {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver","/Chromedriver/chromedriver.exe");

        WebDriver driver=new ChromeDriver();
        driver.get("https://tradeville.eu/");

        driver.findElement(By.id("ac--two")).click();

        WebElement utilizator = driver.findElement(By.id("inputLogin"));
        utilizator.click();
        utilizator.sendKeys("Pradu");

        WebElement password =driver.findElement(By.xpath("//*[@id="ctl00_phContent_ucComposeLogin_ucLoginStartrade_pnlLoginStartrade"]/input[2]"));
        password.click();
        password.sendKeys("");//should input password

        driver.findElement(By.xpath("//*[@id="ctl00_phContent_ucComposeLogin_ucLoginStartrade_btnLogin"]")).click();
    }
}

Advertisement

Answer

There are multiple ways to avoid password plain in your tests, here are few workarounds used by myself:

1.Password as environment variable – in your test you will load the password from your env and use key events in order to paste the password instead of typing it. (paste fills the input with while the send keys will show you each character like typing)

//get password value from env
String pass = System.getenv("STRING_NAME");

//put the value in the clipboard - this avoid the copy action
StringSelection stringSelection = new StringSelection(pass);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);

//paste the value in your password field 
Actions act = new Actions(driver);
act.moveToElement("PASS_INPUT_LOCATOR").click().build().perform();
act.keyDown(Keys.CONTROL).sendKeys("v").keyUp(Keys.CONTROL).build().perform();

2.Avoid login page – save Cookies at first run and then use them to access your page – here is a nice tutorial on how to do it

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement