英文:
Automating a login with out providing the password via selenium Webdriver
问题
以下是翻译好的部分:
我正在尝试创建一个自动化测试,针对我的一个账户。我已经成功做到了,但是我必须通过driver.sendKeys()
方法提供密码。有没有办法可以在不提供密码的情况下自动化这部分呢?我已经附上了下面的代码。
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("");//应输入密码
driver.findElement(By.xpath("//*[@id=\"ctl00_phContent_ucComposeLogin_ucLoginStartrade_btnLogin\"]")).click();
}
}
英文:
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();
}
}
答案1
得分: 0
有多种方法可以避免在测试中使用明文密码,以下是我自己使用的一些解决方法:
-
将密码作为环境变量 - 在测试中,您将从环境中加载密码,并使用键盘事件粘贴密码,而不是键入密码。(粘贴会将输入框填充,而发送键则会像键入一样显示每个字符)
// 从环境中获取密码值
String pass = System.getenv("STRING_NAME");// 将值放入剪贴板 - 这避免了复制操作
StringSelection stringSelection = new StringSelection(pass);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);// 将值粘贴到密码字段中
Actions act = new Actions(driver);
act.moveToElement("PASS_INPUT_LOCATOR").click().build().perform();
act.keyDown(Keys.CONTROL).sendKeys("v").keyUp(Keys.CONTROL).build().perform(); -
避免登录页面 - 在第一次运行时保存 Cookies,然后在访问页面时使用它们 - 这里有一个很好的教程,告诉您如何操作
英文:
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
答案2
得分: 0
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class LoginToTradeville {
public static void main(String[] args) {
String data = null;
File myObj = new File("C:\\Users\\Radu\\Desktop\\java\\Tradeville.txt");
if (myObj.exists()){
try {
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
data = myReader.nextLine();
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}else{
System.out.println("Wrong file");
return;
}
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(String.valueOf(data));
driver.findElement(By.xpath("//*[@id=\"ctl00_phContent_ucComposeLogin_ucLoginStartrade_btnLogin\"]")).click();
}
}
英文:
After some research i opted to read the password from a .txt file on my computer which i will not upload to GIT:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class LoginToTradeville {
public static void main(String[] args) {
String data = null;
File myObj = new File("C:\\Users\\Radu\\Desktop\\java\\Tradeville.txt");
if (myObj.exists()){
try {
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
data = myReader.nextLine();
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}else{
System.out.println("Wrong file");
return;
}
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(String.valueOf(data));
driver.findElement(By.xpath("//*[@id=\"ctl00_phContent_ucComposeLogin_ucLoginStartrade_btnLogin\"]")).click();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论