Thursday, September 25, 2014

Setup Android Test Environment in macmini

 Setup Test Environment in mac
1. Install JDK
2. Install jenkins
3. Install Android studio
4. Install gradle, maven
5. Install firefox, chrome -> disable all warnings, disable auto update
6. Jenkins -> Manage Jenkins -> Manage Plugins -> Install the plugins Android emulator, dashboard view, Email-ext , git, gradle, groovy, Junit attachments, mailer, maven project, selenium plugin
7. Install Virtual Box, Geny motion(Android emulator) and keep the emulator open always
8. Jenkins -> Manage Jenkins -> Configure system
a). Selenium grid port should be 4444
b). Android SDK root should be /Users/Shared/Jenkins/android
c). JDK -> Set JAVA_HOME path /Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home
d). Git -> set path to git executable /usr/bin/git
e). Gradle -> Install automatically
f). Maven -> Set MAVEN_HOME path  /usr/local/apache-maven-3.1.1
g). Extended email Notification
i).  Set SMTP server -> smtp.gmail.com (If you use gmail account to send mails from)
ii). Click use SMTP Authentication -> Enter username and password
9. Setup Selenium Grid: Jenkins -> Selenium Grid -> Configurations -> New Configuration
a). Type -> Custom webdriver node configuration
    i).  Preferred port 5555
    ii). Firefox -> Maximun instances 10
    iii). Chrome -> Maximun instances- 5 , Version- 34.0.1847.116, Chrome binary path-  /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    iv). Add Browser -> Safari -> Maximun instances- 5 , Version- 6.0.5
b) Jenkins -> Selenium Grid -> Hub Management -> Restart
10. To get customised emails, follow the below steps while confiuring the job
a). Editable email Notification section -> Content Type -> HTML
b). Editable email Notification section -> Default Content-> ${SCRIPT, template="custom_final.groovy"}
Jenkins will refer the template in the path /Users/Shared/Jenkins/Home/email-templates/custom_final.groovy
You can use the above template to receive customised mails.
b). Editable email Notification section -> Attchments ->  **/target/surefire-reports/emailable-report.html
11. Configure jobs to point to git repo git@git.com:YourRepository.git and under Build keep Root POM as parameterisedpom.xml
12. Make Build trigger to build periodically "15 7 * * * " this build the job at 7.15 am everyday.

Saturday, September 20, 2014

Send Email Programmatically

Use the below snippet to send email, programmatically. You could use this to send your reports after execution.

package mslt.verification;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.testng.annotations.Test;
public class Sendemail {
@Test
public void sendemailWithAttachments() {
final String username = "YOUR USER NAME";
final String password = "YOUR PASSWORD";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("STEVE@APPLE.HEAVEN"));
message.setSubject("THANK YOU");
message.setText("PFA");
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
messageBodyPart = new MimeBodyPart();
String file = "/Users/admin/Documents/THANKYOU.NOTE";
String fileName = "THANKYOU.NOTE";
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
System.out.println("Sending");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}

Archive Test Reports

Archive test reports using the following snippet. Have this in AfterTest or AfterClass, depends on your requirement 

//        It archives test reports  - author @k.s.karthikeyan@gmail.com
package mslt.verification;

import java.io.File;
import java.io.IOException;

import org.apache.maven.wagon.util.FileUtils;
import org.testng.annotations.Test;

public class ArchiveReport  {

    @Test
      public void archiveTestReport() throws IOException {
            File source = new File(System.getProperty("user.dir")+"/test-output/emailable-report.html");
            File destination = new File("/Users/admin/Desktop/emailable-report_"+System.currentTimeMillis()+".html");
            FileUtils.copyFile(source, destination);
            System.out.println("Report copied to the path "+destination);
    }
}

Monday, September 8, 2014

TestNG - Data Provider Magic

In my experience, I have used data provider to the core. It's just awesome if you want to iterate through a single flow with multiple data. Your data source could be from any form, like excel, csv.. Just follow the steps to pass data to any test method.

@DataProvider(name = "dataSupplier", parallel = false)
public Object[][] data() throws Exception {
Object[][] retObjArr={{"7165"},{"7166"},{"7167"},{"7168"},{"7169"},{"7170"},{"7171"},{"7172"},{"7173"},{"7174"},{"7175"},{"7176"},{"7177"},{"7178"}};
return(retObjArr);
}


@Test(dataProvider = "dataSupplier")       // Here I am telling the test case to use the dataprovider "dataSupplier"
public void test1(String s) throws InterruptedException {
System.out.println(s);
}

You are done..!!!  It now prints all the numbers if you run the test case once and you could see an excellent report which says 14 tests passed :)


Same way you could do this with excel by using apache poi or jxl api like the below example.

@DataProvider(name = "data-provider", parallel = false)
public Object[][] data() throws Exception {
Object[][] retObjArr=getTableArray("C:\\QA\\ws\\nu\\src\\test\\resources\\test.xls");
return(retObjArr);
}


public String[][] getTableArray(String xlFilePath) throws Exception{
String[][] tabArray=null;
Workbook workbook = Workbook.getWorkbook(new File(xlFilePath));
Sheet wrksheet = workbook.getSheet(0);

String tabdata[][] = new String[wrksheet.getRows()][wrksheet.getColumns()];
for (int i = 1; i < wrksheet.getRows(); i++) {
for (int j = 0; j < wrksheet.getColumns(); j++) {
tabdata[i][j] = wrksheet.getCell(j, i).getContents();
}
}
return(tabdata);
}
Here you are returning the data as objects.. Look at the below test which uses this data-provider and how it receives the data. It supplies 2 strings - link and response code from the data provider.

@Test(dataProvider="data-provider")
public final void test2(String link, String responsecode) throws InterruptedException {
if(link != null){
Assert.assertNotEquals(responsecode,"404");}
}