Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Data collection of protected web page with WebDriver and Redis as transport for receiving pass code
package se.wendt.statoil.mastercard;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;
public class DataProvider {
public static boolean DEBUG = false;
public static void main(String[] args) {
DataProvider dataProvider = new DataProvider(new NumberParser());
System.out.println(dataProvider);
}
private final NumberParser numberParser;
private Integer balance;
private Integer limit;
private Integer disposable;
private WebDriver browser;
private Jedis redis;
private Collection<String> passCodeRef;
private CountDownLatch latch;
public DataProvider(NumberParser numberParser) {
this.numberParser = numberParser;
processPage();
}
@Override
public String toString() {
return String.format("DataProvider[%s,%s,%s]", balance, disposable, limit);
}
public int getBalance() {
return balance;
}
public int getLimit() {
return limit;
}
public int getDisposable() {
return disposable;
}
private void processPage() {
log("Creating browser");
browser = WebDriverFactory.createFirefoxBrowser();
try {
browser.navigate().to("https://application.sebkort.com/nis/external/stse/login.do");
startListeningToRedisChannel();
requestSmsPassCode();
// now wait for message via redis
String passCode = getPassCodeFromSmsViaJedis();
log("Pass code is '%s'", passCode);
WebElement passCodeElement = browser.findElement(By.id("TMPPASSWORD"));
passCodeElement.sendKeys(passCode);
passCodeElement.submit();
log("Parsing page");
limit = getIntegerValue(".Welcomepagebillingunitlastcreditamount");
disposable = getIntegerValue(".Welcomepagebillingunitlastdisposableamount");
balance = getIntegerValue(".welcomepagebillingunit2rowcol2");
} finally {
log("Closing browser");
browser.close();
log("Browser closed");
log("Closing redis");
redis.quit();
log("Redis closed");
}
}
private void startListeningToRedisChannel() {
log("Attempting to listen to redis");
redis = new Jedis(System.getEnv("REDIS_SERVER"));
passCodeRef = new ArrayList<String>();
latch = new CountDownLatch(1);
new Thread(new Runnable() {
@Override
public void run() {
JedisPubSub pubSub = new JedisPubSub() {
@Override
public void onUnsubscribe(String arg0, int arg1) {
}
@Override
public void onSubscribe(String arg0, int arg1) {
log("Now listening to redis");
}
@Override
public void onPUnsubscribe(String arg0, int arg1) {
}
@Override
public void onPSubscribe(String arg0, int arg1) {
}
@Override
public void onPMessage(String arg0, String arg1, String arg2) {
}
@Override
public void onMessage(String channel, String message) {
log("Message recevied");
if (numberParser.messageContainsPassCode(message)) {
passCodeRef.add(numberParser.parsePassCodeFromSms(message));
log("Got pass code");
latch.countDown();
log("Latch released");
this.unsubscribe();
}
}
};
redis.subscribe(pubSub, "sms");
}
}).start();
}
private String getPassCodeFromSmsViaJedis() {
try {
log("Waiting for code");
latch.await(3, TimeUnit.MINUTES);
log("waiting ended");
} catch (InterruptedException e) {
log("waiting interrupted");
}
if (passCodeRef.size() == 0) {
throw new IllegalStateException(
"Didn't get pass code via jedis via php via 46elks via SMS, within 3 minutes");
}
return passCodeRef.iterator().next();
}
private void requestSmsPassCode() {
WebElement element = browser.findElement(By.id("SMSuname"));
element.sendKeys(System.getEnv("SMSUNAME"));
element = browser.findElement(By.id("SMSPASSWORD"));
element.sendKeys(System.getEnv("SMSPASSWORD"));
element.submit();
}
private int getIntegerValue(String selector) {
int value = 0;
WebElement e = browser.findElement(By.cssSelector(selector));
value = numberParser.parseInt(e.getText());
return value;
}
private void log(String msg, Object... args) {
if (DEBUG) {
System.out.printf(msg + "\n", args);
}
}
}
ceda@filserver:~/workspace/statoil-mastercard$ DEBUG=true java -jar target/statoil-mastercard-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Creating browser
Attempting to listen to redis
Now listening to redis
Waiting for code
Message recevied
Got pass code
Latch released
waiting ended
Pass code is 'D5JL98'
Parsing page
Closing browser
Browser closed
Closing redis
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: It seems like server has closed the connection.
at redis.clients.util.RedisInputStream.readLine(RedisInputStream.java:91)
at redis.clients.jedis.Protocol.processMultiBulkReply(Protocol.java:110)
at redis.clients.jedis.Protocol.process(Protocol.java:63)
at redis.clients.jedis.Protocol.read(Protocol.java:122)
at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:152)
at redis.clients.jedis.Jedis.quit(Jedis.java:75)
at se.wendt.statoil.mastercard.DataProvider.processPage(DataProvider.java:85)
at se.wendt.statoil.mastercard.DataProvider.<init>(DataProvider.java:37)
at se.wendt.statoil.mastercard.MainTask.main(MainTask.java:17)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.