Friday 24 July 2015

HIghlighting the element using java script.

for (int iCnt = 0; iCnt < 3; iCnt++) {
       //Execute javascript
       js.executeScript("arguments[0].style.border='2px groove red'", driver.findElement(By.xpath("//div[starts-with(text(), '11.0')]")));
       try {
              Thread.sleep(500);
       } catch (InterruptedException e) {
               e.printStackTrace();
       }
       js.executeScript("arguments[0].style.border=''", driver.findElement(By.xpath("//div[starts-with(text(), '11.0')]")));
        }
}

Thursday 23 July 2015

Selenium Question & Answers

1)  List the advantages of Webdriver over Selenium Server?
  • If you are using Selenium-WebDriver, you don’t need the Selenium Server as it is using totally different technology
  • Selenium Server provides Selenium RC functionality which is used for Selenium 1.0 backwards compatibility
  • Selenium Web driver makes direct calls to browser using each browsers native support for automation, while Selenium RC requires selenium server to inject Javascript into the browser
2)  Mention what are the capabilities of Selenium WebDriver or Selenium 2.0 ?
WebDriver should be used when requiring improvement support for
  • Handling multiple frames, pop ups , multiple browser windows and alerts
  • Page navigation and drag & drop
  • Ajax based UI elements
  • Multi browser testing including improved functionality for browser not well supported by Selenium 1.0
3)  While injecting capabilities in webdriver to perform tests on a browser which is not supported by a webdriver what is the limitation that one can come across?
Major limitation of injecting capabilities is that “findElement” command may not work as expected.
4)  Explain how you can find broken images in a page using Selenium Web driver ?
To find the broken images in a page using Selenium web driver is
  • Get Xpath and get all the links in the page using tag name
  • In the page click on each and every link
  • Look for 404/500 in the target page title
5)  Explain how you can handle colors in web driver?
To handle colors in web driver you can use
Use getCssValue(arg0) function to get the colors by sending ‘color’ string as an argument
image010

6)  Using web driver how you can store a value which is text box?
You can use following command to store a value which is text box using web driver
driver.findElement(By.id(“your Textbox”)).sendKeys(“your keyword”);

7)  Explain how you can switch between frames?
To switch between frames webdrivers [ driver.switchTo().frame() ] method takes one of the three possible arguments
  • A number:  It selects the number by its (zero-based) index
  • A number or ID: Select a frame by its name or ID
  • Previously found WebElement: Using its previously located WebElement select a frame
8)  Mention 5 different exceptions you had in Selenium web driver?
The 5 different exceptions you had in Selenium web drivers are
  • WebDriverException
  • NoAlertPresentException
  • NoSuchWindowException
  • NoSuchElementException
  • TimeoutException
9)  Explain using Webdriver how you can perform double click ?
You can perform double click by using
  • Syntax- Actions act = new Actions (driver);
  • act.doubleClick(webelement);
10) How will you use  Selenium to upload a file ?
You can use “type”command to type in a file input box of upload file. Then, you have to use “Robot” class in JAVA to make file upload work.
11) Which web driver implementation is fastest?
HTMLUnit Driver implementation is fastest, HTMLUnitDriver does not execute tests on browser but plain http request, which is far quick than launching a browser and executing tests

12) Explain how you can handle frames using Selenium 2.0 ?
To bring control on HTML frame you can use “SwitchTo” frame method-

driver.switchTo().frame(“frameName”);
To specify a frame you can use index number
driver.switchTo().frame(“parentFrame.4.frameName”);
This would bring control on frame named- “frameName” of the 4th sub frame names “parentFrame”
13) What is the difference between getWindowhandles() and getwindowhandle() ?
getwindowhandles(): It is used to get the address of all the open browser and its return type is iterator<String>
getwindowhandle(): It is used to get the address of the current browser where the control is and return type is string
14) Explain how you can switch back from a frame?
To switch back from a frame use method defaultContent()
Syntax-driver.switchTo().defaultContent();
15) List out different types of locators?
Different types of locators are
  • By.id()
  • By.name()
  • By.tagName()
  • By.className()
  • By.linkText()
  • By.partialLinkText()
  • By.xpath
  • By.cssSelector()
16) What is the command that is used in order to display the values of a variable into the output console or log?
  • In order to display a constant string, command can be used is echo <constant string>
  • If order to display the value of a variable you can use command like echo ${variable name>>
Above is using PHP. If you are using Java, replace echo with System.out.println
17)   Explain how you can use recovery scenario with Selenium?
Recovery scenarios depends upon the programming language you use.  If you are using Java then you can use exception handling to overcome same.  By using “Try Catch Block” within your Selenium WebDriver Java tests
18) Explain how to iterate through options in test script?
To iterate through options in test script you can loop features of the programming language, for example to type different test data in a text box you can use “for” loop in Java
// test data collection in an array
String[ ] testData = { “test1” , “test2” , “test3” } ;
// iterate through each test data
For  (string s: test data) { selenium.type ( “elementLocator”, testData) ; }
19) How can you prepare customized html report using TestNG in hybrid framework ?
There are three ways
  • Junit: With the help of ANT
  • TestNG: Using inbuilt default.html to get the HTML report. Also XST reports from ANT, Selenium, TestNG combinations
  • Using our own customized reports using XSL jar for converting XML content to HTML
20) From your test script how you can create html test report?
To create html test report there are three ways
  • TestNG:  Using inbuilt default.html to get the HTML report. Also XLST reports from ANT, Selenium, TestNG combination
  • JUnit: With the help of ANT
  • Using our own customized reports using XSL jar for converting XML content to HTML

Tuesday 21 July 2015

Email sending with attachment.

Required jar file : javax.mail-1.4.jar
 use this link : http://www.java2s.com/Code/Jar/j/Downloadjavaxmailjar.htm
           or
http://mvnrepository.com/artifact/javax.mail/mail/1.4.7


package testNG;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
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;

public class EmailAttachmentSender {  public static void main(String[] args) {
    // Recipient's email ID needs to be mentioned.
    String to="testing3.me@gmail.com";//change accordingly 
    final String user="testing3.me@gmail.com";//change accordingly 
    final String password="*****";//change accordingly 
    
    //1) get the session object    
    Properties properties = System.getProperties(); 
   /* properties.setProperty("mail.smtp.host", "smtp.gmail.com"); 
    properties.put("mail.smtp.auth", "true");  */
   
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.socketFactory.port", "465");
    properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.port", "465");
   
    Session session = Session.getDefaultInstance(properties, 
     new javax.mail.Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication(user,password); 
     } 
    }); 
      
    //2) compose message    
    try{ 
      MimeMessage message = new MimeMessage(session); 
      message.setFrom(new InternetAddress(user)); 
      message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); 
      message.setSubject("Message Aleart"); 
       
      //3) create MimeBodyPart object and set your message text    
      BodyPart messageBodyPart1 = new MimeBodyPart(); 
      messageBodyPart1.setText("This is message body"); 
       
      //4) create new MimeBodyPart object and set DataHandler object to this object     
      MimeBodyPart messageBodyPart2 = new MimeBodyPart(); 
   
      String filename = "C:\\Users\\Youtility\\Desktop\\MyLogFile.log";//change accordingly 
      DataSource source = new FileDataSource(filename); 
      messageBodyPart2.setDataHandler(new DataHandler(source)); 
      messageBodyPart2.setFileName(filename); 
      
      
      //5) create Multipart object and add MimeBodyPart objects to this object     
      Multipart multipart = new MimeMultipart(); 
      multipart.addBodyPart(messageBodyPart1); 
      multipart.addBodyPart(messageBodyPart2); 
   
      //6) set the multiplart object to the message object 
      message.setContent(multipart ); 
      
      //7) send message 
      Transport.send(message); 
    
     System.out.println("message sent...."); 
     }catch (MessagingException ex) {ex.printStackTrace();} 
   } 
}

Log file generating and posting data

package testNG;

import java.io.IOException;
import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.testng.annotations.Test;


public class AssertionsTest {

   
    @Test
    public void assertionTest(){
       
         Logger logger = Logger.getLogger("MyLog");
            Appender fh = null;
            try {
                fh = new FileAppender(new SimpleLayout(), "C:\\Users\\Youtility\\Desktop\\MyLogFile.log");
                logger.addAppender(fh);
                fh.setLayout(new SimpleLayout());
                logger.info("My first log");
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            logger.info("Hi How r u?");
       
        System.out.println("Completed.");
    }
}