I am trying to prevent the credit card number from displaying in the screen while playing the selenium webdriver test. I am pulling the credit card number from central database, and is not found in the test. To make it secure, I want to mask the card number while selenium tries to sendKeys
the number in credit card number input field. I tried different options, but they tend to change the actual value of the card which throws card number error in the page. Are there any ideas on how can I mask actual card without impacting the real value in selenium.
Here is what I did:
String creditCardNumber = "1234567891234567";
driver.findElement(By.id("label")).sendKeys(
maskCardNumber(creditCardNumber, "xxxxxxxxxxxxxxx"));
public static String maskCardNumber(String cardNumber, String maskedCard) {
// format the number
int index = 0;
StringBuilder maskedCardNumber = new StringBuilder();
for (int i = 0; i < maskedCard.length(); i++) {
char c = maskedCard.charAt(i);
if (c == '#') {
maskedCardNumber.append(cardNumber.charAt(index));
index++;
} else if (c == 'x') {
maskedCardNumber.append(c);
index++;
} else {
maskedCardNumber.append(c);
}
}
// return the masked number
return maskedCardNumber.toString();
}