Saturday, 30 March 2019

How to write xPath


There are Two Types of Xpath
1)Absolute Xpath
2)Relative Xpath
1)Absolute Xpath:
a) It is a direct way to locate the Element.
b) It start with Single Element(/)which means it try to search the Element from the Root.
Example:
/html/body/equote-root/equote-risk/div/div/div[1]/equote-area-sorter/div/equote-panel/div/div[2]/div/div[2]/button
2)Relative Xpath:
a) It starts from the Middle of the HTML.
b) Starts with double slash (//)which means it try to search the Element anywhere in the DOM.
c)It is flexible compare to Absolute Xpath.
Example:
//span[contains(text(),'Submit Button')]


 How to Write a Smart XPaths for Complex and Dynamic Elements

1)Use Contains:
When a Element is Dynamic it is easy to find the Elements.
Example:
//*[contains(@name,'userName')]
//*[contains(@href,'mercurywelcome.php?osCsid=eb1d150686047b0beb894f5ec0313408')]
//*[contains(@text,'here')]




2)Use OR & AND

Using OR when both the condition is true it find the Element, when there is one condition is true another one is false in that time also it find the Element.
Example:
//*[@name='pass' or @name='login']


Using AND when both the condition is true it find the element otherwise it fails to find the element.

//*[@name='password' or @name='login']



3)Starts-with
Match with starting text of the attribute is used to find the element whose changes dynamically.
Example:
//input[starts-with(@name,'principal')]



4)Text

Find the element with the exact text match.
Example:
//a[text()='REGISTER']



Xpath Axes Method:

a) Following
b) Ancestor
C) Child
d) Preceding
e) Following-sibling
f) Parent
g) Self
h) Descendant

a) Following:

To select all the elements from the node.
Example:
//*[@type='text']//following::input
With the input[1] it find the particular element.
//*[@type='text']//following::input[1]




b) Ancestor
First It finds the text which is ‘Enterprise Testing’
Then start to find the elements in node.
Example:
//*[text()='Enterprise Testing']//ancestor::div

c) Child
It select all the children elements from the node.
Example:
//*[@name='country']/child::option
//*[@name='country']/child::option[150]


d)Preceding

It selects all the node come before the current node.
Example:
//*[@type='image']//preceding::input
//*[@type='image']//preceding::input[10]








e) Sibling:
It selects the following sibling of the context node.

Example:
//*[@type='text']//following-sibling::input
//*[@type='text']//following-sibling::input[1]

f) Parent
It selects the parent of the current node
Example:
//*[@id='rt-feature']//parent::div
//*[@id='rt-feature']//parent::div[1]

g) Self
It select the current node.
Example:
//*[@type='password']//self::input
//*[@type='password']//self::input[1]


h)Descendant
Example:        
//*[@id='test']//descendant::a


Wednesday, 27 February 2019

Challenges to face run selenium sripts in IE Browser


Challenges to face run selenium sripts in IE Browser
1)Path of the IE Driver
The path to the driver executable must be set by the webdriver.ie.driver system property; for more information, see http://code.google.com/p/selenium/wiki/InternetExplorerDriver. The latest version can be downloaded from http://selenium-release.storage.googleapis.com/index.html
Class IE
{
Public static void main(String args[])
{
WebDriver t=new InternetExplorerDriver();
t.get(“”);
}
}



This Error happened because of there is no third browser in your system,you have to download the IE third party browser from the site seleniumhq.org
Filename:IEDriverServer.exe 32 bit or 64 bit




public class IE {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.ie.driver","D:\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe");
WebDriver t=new InternetExplorerDriver();
t.get("https://www.seleniumhq.org/");


}

}
Output:After set the third party browser in the path

2)Enable the Proteted Mode:
How to set the Protected Mode settings in IE Browser
1) Go to Tools > Internet Options and Under Internet Options click on the Security tab.
2) Click on the Internet zone to Select a zone and to view its Protected Mode property.
3) Now check the Enable Protected Mode check-box. Enable the Protected now.

Note:Does not Enable the Protected mode , when you try to run the program it show the error in your console window



3)Untrusted SSL Certifiate
IE is most secured browser at time using IE browser with selenium it gives SSL certificate popup
To resolve the issue handled in two ways:


Solution1:

public class IE {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.ie.driver","D:\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe");
WebDriver t=new InternetExplorerDriver();
t.get("https://www.seleniumhq.org/");
t.navigate().to("javascript:document.getElementById('overridelink').click()");


}

}

Solution 2:
Desiried Capabilities:

public class IE {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.ie.driver","D:\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe");
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();

// Settings to Accept the SSL Certificate in the Capability object
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

WebDriver t=new InternetExplorerDriver();
t.get("https://www.seleniumhq.org/");
t.navigate().to("javascript:document.getElementById('overridelink').click()");


}

}


Friday, 25 January 2019

Email Attachment in Selenium through Gmail ID using Apache Commons Email Concept


1)To create a Maven Projet.

2)Add the Dependency Commons-Email in POM.xml file.

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-email</artifactId>
    <version>1.3.1</version>
</dependency>

3.Create a class and implement the code
package Sampletester;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;

public class NewTestEmailForm {

     public static void main(String[] args) throws EmailException {
           // TODO Auto-generated method stub
             EmailAttachment attachment = new EmailAttachment();
             attachment.setPath("D:\\test11.png");
             attachment.setDisposition(EmailAttachment.ATTACHMENT);
             attachment.setDescription("Picture of test");
             attachment.setName("test report");

             // Create the email message
             MultiPartEmail email = new MultiPartEmail();
             email.setHostName("smtp.gmail.com");
             email.setSmtpPort(465);
             email.setAuthenticator(new DefaultAuthenticator("yourgmailID@gmail.com", "yourgmailpassword"));
             email.setSSLOnConnect(true);
             email.setFrom("yourgmailID@gmail.com");
             email.setSubject("TestMail Attachment");
             email.setMsg("This is a test mail ...for attahment:-)");
             email.addTo("reeiveremail address@gmail.com");
            

             // add the attachment
             email.attach(attachment);

             // send the email
             email.send();
            
             System.out.println("report");
     }

}


4.Open the Gmail Account and Check your Attachment.


Take Screenshot of Entire Page using Shutterbug in Selenium WebDriver 3


Scenario 1:
-----------
When Try with selenium-server-standalone-3.14.jar to take the screenshot.

Refer the code:
---------------

File scrFile = ((TakesScreenshot)d).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\path\\screenshot.png"));

Scenario 2:
-----------

When Try with selenium-server-standalone-3.141.59(Latest Version).jar to take the screenshot.

It doesnot support the FileUtils.copyFile replace that it support Files.copy

File scrFile = ((TakesScreenshot)d).getScreenshotAs(OutputType.FILE);
Files.copy(scrFile, new File("D:\\path\\screenshot.png"));

Note:
Both scenario 1 and scenario 2 it does not take the screenshot of entire page.

Scenario 3:
-----------
To Take the Entire page of screenshot.

1)To create a Maven Projet.

2)Add the Dependency Selenium-shutterbug in POM.xml file.

<!-- https://mvnrepository.com/artifact/com.assertthat/selenium-shutterbug -->
<dependency>
    <groupId>com.assertthat</groupId>
    <artifactId>selenium-shutterbug</artifactId>
    <version>0.9</version>
</dependency>

3)Create a Class and Implement the Code.

package Screen;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import com.assertthat.selenium_shutterbug.core.Shutterbug;
import com.assertthat.selenium_shutterbug.utils.web.ScrollStrategy;

public class Take {

public static void main(String[] args) {
           // TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","D:\\chromedriver_win32\\chromedriver.exe");
       
WebDriver d = new ChromeDriver();
d.get("https://www.seleniumhq.org/download/");
       Shutterbug.shootPage(d,ScrollStrategy.BOTH_DIRECTIONS,500,true).withName("FullPageScreenshot").save();

        d.quit();
     }

}

4)After exeute the code Right click the project folder and refersh it then it generate the folder "Screenshot" and open your screenshot file.


Output of the Program: