Automating a login with out providing the password via selenium Webdriver

Tags: , ,

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;

public class LoginToTradeville {
    public static void main(String[] args) {

        WebDriver driver=new ChromeDriver();


        WebElement utilizator = driver.findElement("inputLogin"));;

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



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);

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

Source: stackoverflow