Page Object pattern with Selenium Webdriver
I’m writing a small framework to help me automate some tests on a web-application. Such tests must run on Firefox, Chrome, Internet Explorer 8 sharing the same codebase. General purpose functionalities, as logging and screenshots, are already implemented.
I’m still looking for a solution on how to map the objects in the page in a reusable way. I found some articles on the Page Object pattern, but no one is as exhaustive as I’d like. To figure out the all pattern and the correct way to use it, I suggest reading through this collection of tutorials:
http://code.google.com/p/selenium/wiki/PageObjects
http://code.google.com/p/selenium/wiki/PageFactory
http://stackoverflow.com/questions/10315894/selenium-webdriver-page-object
http://www.summa-tech.com/blog/2011/10/10/using-page-objects-with-selenium-and-web-driver-20/
http://chon.techliminal.com/page_object/#/intro
And some tips on the PageFactory
http://sqa.stackexchange.com/questions/571/page-objects-design-issues/1500#1500
Finite Automata
Today I stumbled upon a very interesting article on StackExchange:
What is the enlightenment I’m supposed to attain after studying finite automata
I really loved all the theoretical courses back in University. They were exciting and thrilling, and kind of rewarding in terms of putting things in a new perspective and thus making me learn.
Profiles
Mi sono iscritto a due ottime risorse online: StackOverflow e IBM DeveloperWorks.
StackOverflow è un sito di FAQ, fondamentalmente uno Yahoo Answers per programmatori! Con tutti gli orpelli che potete desiderare… badges, punteggi, wiki, obiettivi di gioco!
Il forum di IBM presenta vari articoli che spiegano i workaround più conosciuti agli assurdi bug che si incontrano -_-’ sui prodotti Rational.
I subscribed to StackOverflow and IBM DeveloperWorks, two grat online resources.
StackOverflow is a FAQ site, a Yahoo Answers for programmers! It has every eyecandy you can think of… badges, reputation scores, wikis, achievements!
IBM forums just keep tracks of workarounds for the most absurd bugs you encounter while using Rational products -_-’
VisualStudio 2005: “Project type is not supported by this installation”
Per prendere in carico la manutenzione di una Applicazione Web sviluppata e utilizzata in azienda, mi è stato installato Microsoft Visual Studio 2005. Apro il progetto ed un irritante popup mi avvisa: “Project type is not supported by this installation”.
Googlando in Internet scopro che il VS2005 “liscio” non supporta le Applicazioni Web, e che necessita di Service Pack 1 e template. I link per scaricarli:
Microsoft Visual Studio 2005 – Update to Support Web Application Projects
Microsoft® Visual Studio® 2005 Team Suite Service Pack 1
Una volta installati, il progetto viene aperto e lanciato correttamente. Lavoro su una macchina con WindowsXP SP3, non è richiesto IIS in quanto VS2005 può utilizzare un suo server web interno.
SSLServerSocket con certificati self-signed
Dopo 3 giorni di patimento alla ricerca di tutorial esaustivi (e diversi da RTFM) ed eccezioni volanti, finalmente ce l’ho fatta a mettere assieme tutti i pezzi necessari.
Un server con un certificato self-signed, un client che si connette e una connessione SSL usando Java Secure Socket Extension.
Sito ufficiale: [] http://java.sun.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html
Primo passo: creazione chiave privata e auto-certificazione da inserire nel KeyStore (server)
keytool -genkeypair -alias mytest -keyalg RSA -validity 7 -keystore server.jks
Secondo passo: esportare il certificato
keytool -export -alias mytest -keystore server.jks -rfc -file server.cer
Terzo passo: importare il certificato in un TrustStore (client)
keytool -import -alias mytest -file server.cer -keystore client.jks
Uso dei KeyStore personali, diversi da quelli standard forniti con la JVM.
L’uso dei socket SSL in Java e’ identico all’uso dei socket standard. La differenza sta nella procedura di setup del socket stesso.
Il seguente codice fa un setup personalizzato e restituisce un SSLServerSocket, pronto per accettare connessioni.
final int _PORT = 5555;
final String _KEYSTORE = "server.jks";
final String _KEYSTORE_PASSWORD = "123456";
private SSLServerSocket setupSSLServerSocket(){
try {
SSLContext sslContext = SSLContext.getInstance( "TLS" );
KeyManagerFactory km = KeyManagerFactory.getInstance("SunX509");
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(_KEYSTORE), _KEYSTORE_PASSWORD.toCharArray());
km.init(ks, _KEYSTORE_PASSWORD.toCharArray());
sslContext.init(km.getKeyManagers(), null, null);
SSLServerSocketFactory f = sslContext.getServerSocketFactory();
SSLServerSocket ss = (SSLServerSocket) f.createServerSocket(_PORT);
return ss;
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Setup di un socket SSL lato client. Il codice rimane quasi identico, tranne l’uso di un TrustManager al posto di un KeyManager.
private final int _PORT = 5555;
private final String _TRUSTSTORE ="client.jks";
private final String _TRUSTSTORE_PASSWORD="client";
private SSLSocket setupSSLClientSocket(){
try {
SSLContext sslContext = SSLContext.getInstance( "TLS" );
KeyStore clientks = KeyStore.getInstance("JKS");
clientks.load(new FileInputStream(_TRUSTSTORE), _TRUSTSTORE_PASS.toCharArray());
TrustManagerFactory tm = TrustManagerFactory.getInstance("SunX509");
tm.init(clientks);
sslContext.init(null, tm.getTrustManagers(), null);
SSLSocketFactory f = sslContext.getSocketFactory();
SSLSocket sslSocket = (SSLSocket) f.createSocket("localhost", _PORT);
return sslSocket;
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Tutto questo codice non fa altro che usare i certificati specificati nei KeyStore personalizzati, senza nessun’altro tipo di configurazione particolare. Il metodo “breve” per fare questa configurazione consiste nel settare due proprieta’ della VM:
SERVER
System.setProperty("javax.net.ssl.keyStore", _KEYSTORE);
System.setProperty("javax.net.ssl.keyStorePassword", _KEYSTORE_PASSWORD);
SSLServerSocketFactory f = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket sslServerSocket = (SSLServerSocket) f.createServerSocket(_PORT);
CLIENT
System.setProperty("javax.net.ssl.trustStore", _TRUSTSTORE);
System.setProperty("javax.net.ssl.trustStorePassword", _TRUSTSTORE_PASSWORD);
SSLSocketFactory f = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslSocket = (SSLSocket) f.createSocket(_HOST, _PORT);
E le successive SSLSocketFactory istanziate useranno i KeyStore indicati.
Per il debug di SSL (ad esempio i certificati caricati in memoria, la procedura di HandShake) da riga di comando esiste lo switch -Djavax.net.debug=ssl.
java -Djavax.net.debug=ssl Applicazione
ATTENZIONE: la procedura di HandShake SSL/TLS NON avviene alla connessione del socket ma prima di iniziare un trasferimento dati, per cui appena connesso il socket segnala come algoritmo di cifratura “SSL_NULL_WITH_NULL_NULL” cioe’ trasmissione in chiaro. Trasmettendo dei dati Server e Client si accorderanno sull’algoritmo da usare.
Shortcuts per Visual C#
PDF stampabile con le scorciatoie da tastiera per Visual C# in Visual Studio 2008 e Visual C# 2008 Express Edition:
http://www.microsoft.com/downloads/details.aspx?familyid=e5f902a8-5bb5-4cc6-907e-472809749973&displaylang=en
Parsing di un documento in XML con Java
Ricopio qui il codice di un metodo per effettuare il parsing di un file XML come documento DOM. Non e’ stato facile capire come fare tutto il procedimento con Java6, e molti degli esempi trovati in rete non funzionavano a dovere.
Purtroppo non ricordo dove lo ho trovato, quindi prego l’autore di scusarmi se non lo menziono!
private void parseXML(InputStream xmlHelpFile) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//factory.setValidating(true);
//factory.setNamespaceAware(true);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(xmlHelpFile);
System.out.println("parsing started");
} catch (SAXParseException spe) {
// Error generated by the parser
System.out.println("\n** Parsing error" + ", line " +
spe.getLineNumber() + ", uri " + spe.getSystemId());
System.out.println(" " + spe.getMessage());
// Use the contained exception, if any
Exception x = spe;
if (spe.getException() != null) {
x = spe.getException();
}
x.printStackTrace();
} catch (SAXException sxe) {
// Error generated during parsing)
Exception x = sxe;
if (sxe.getException() != null) {
x = sxe.getException();
}
x.printStackTrace();
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
pce.printStackTrace();
} catch (IOException ioe) {
// I/O error
ioe.printStackTrace();
}
}