0
我寫了一個測試來提取需要更改的登錄名。數據(登錄名和密碼)我從數據庫下載並保存到文件中。稍後,使用for循環,我選擇每個登錄名並更改密碼。但是,對於每次登錄,都會分配一個新密碼。我希望所有登錄都能獲得相同的密碼。因此,RandomStringUtils函數執行一次,每個登錄都獲得相同的密碼。有人能幫助我嗎?隨機密碼生成selenium webdriver java
public class ReadData {
**String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String character = "[email protected]#$%^&*-_=+|;:,<.>/?";**
public void tc() throws IOException, InterruptedException, InvalidFormatException, SQLException, ClassNotFoundException{
Connection connection = null;
PreparedStatement preparedStatement = null;
WebElement button;
File fileProperties = new File("db.properties");
FileInputStream fileInput = null;
try {
fileInput = new FileInputStream(fileProperties);
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
}
Properties prop = new Properties();
// load properties file
try {
prop.load(fileInput);
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
WebDriver driver = new FirefoxDriver();
driver.get(prop.getProperty("URLtoPage"));
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
ArrayList<String> userName = readExcelData(0);
ArrayList<String> password = readExcelData(1);
**for(int i =0;i<userName.size();i++){**
driver.findElement(By.id("userId")).sendKeys(userName.get(i));
// driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys(password.get(i));
Thread.sleep(1000);
// driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
driver.findElement(By.id("Submit")).click();
// Thread.sleep(3000);
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MILLISECONDS);
List<WebElement> myElemnt = driver.findElements(By.xpath(".//*[@id='_id9']/table/tbody/tr[2]/td[1]/table[1]/tbody/tr[5]/td[2]/a"));
if(myElemnt.size() == 0){
Class.forName("org.postgresql.Driver");
System.out.println("PostgreSQL JDBC Driver Registered!");
// Open a connection
System.out.println("Connecting to a selected database...");
connection = DriverManager.getConnection(prop.getProperty("URL"), prop.getProperty("username"), prop.getProperty("password"));
System.out.println("Connected database successfully...");
preparedStatement = connection.prepareStatement("update \"agent_external_system_access\" set disabled='t', external_password = ? where external_login = ? AND product_category_id = 42;");
preparedStatement.setString(1, password.get(i));
preparedStatement.setString(2, userName.get(i));
// execute update SQL statement
preparedStatement.executeUpdate();
System.out.println("Record is updated to table!");
driver.findElement(By.id("userId")).clear();
driver.findElement(By.id("password")).clear();
}
else {
//driver.navigate().refresh();
System.out.println("Account unlocked");
System.out.println("We lock the account for password change time - update to the database");
connection = DriverManager.getConnection(prop.getProperty("URL"), prop.getProperty("username"), prop.getProperty("password"));
preparedStatement = connection.prepareStatement("update \"agent_external_system_access\" set disabled='t', external_password = ? where external_login = ? AND product_category_id = 42;");
preparedStatement.setString(1, password.get(i));
preparedStatement.setString(2, userName.get(i));
// execute update SQL statement
preparedStatement.executeUpdate();
System.out.println("Record is updated to table!");
// control change password
driver.findElement(By.xpath(".//*[@id='_id9']/table/tbody/tr[2]/td[1]/table[1]/tbody/tr[5]/td[2]/a")).click();
// control Password
driver.findElement(By.id("password")).sendKeys(password.get(i));
// control new password
**String pwd = RandomStringUtils.random(15, upper+character);
System.out.println("New password: " + pwd);
driver.findElement(By.id("newPassword")).sendKeys(pwd);
driver.findElement(By.id("confirmPassword")).sendKeys(pwd);
System.out.println("Confirm password: " + pwd);**
driver.findElement(By.xpath(".//*[@id='_id9']/table/tbody/tr[2]/td[2]/table[2]/tbody/tr[2]/td/table/tbody/tr/td[1]/button")).click();
} // close for
driver.quit();
} // close method tc();
} // close class ReadData