Showing posts with label SELENIUM. Show all posts
Showing posts with label SELENIUM. Show all posts

Algorithms

Check if prime or not
1) int i,m,n, Flag = 0
2) n = user input
3) m = n/2 or Sqrt(n)
4) if n <=1 then not prime
5) loop  i = 2 and i<=m
6) if (n%i==0) , Not prime, Flag = 1, break loop
7) if flag = 0, Number is prime


 Check if prime or not between two numbers;
1) input start and end
2) Run loop between 2
3) create a function isPrime(i) and print if prime.

Isprime(i) algorithm
1) int i,m,n
2) n = user input
3) m = n/2 or Sqrt(n)
4) if n <=1 then not prime
5) loop  i = 2 and i<=m
6) if (n%!=0) , Not prime, Flag = 1, break loop
7) if flag = 0, prime.



 Find the factors of numbers
1) Get the number - should not be prime
2) loop fromi =  2 to number/2
3) if number%i = 0, then print i

 Factorial of Number

1) get the number
2) int fact = 1
3) for i = i to number
4) fact = fact * i print fact

TakeScreenshot on Failure Selenium

Code


Take ScreenShot in Selenium

Code to take screenshot


The "." sign will make sure that the screenshot is stored in current test run directory

Here Screenshot folder has been added to the test. And the screen shot will be saved here.


Main Code:

public void tcu() throws Exception{
            driver.manage().window().maximize();
            driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
            TakesScreenshot ts = (TakesScreenshot)driver;
            File source = ts.getScreenshotAs(OutputType.FILE);
            FileHandler.copy(source, new File("./screenshot/na.jpg"));
            driver.close();
           
    }

Working with xpath.

WebTable are most challenging.

Partial text match:

 HTML text
<div class="ui-grid-cell-contents ng-binding ng-scope">Archana</div>

XPATH
//div[contains(text(),'Archana')]

Exact Text match

HTML text
<div class="ui-grid-cell-contents ng-binding ng-scope">Archana</div>

XPATH
//div[text()='Archana']

Navigating from child to parent

HTML text
<div ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.uid" ui-grid-one-bind-id-grid="rowRenderIndex + '-' + col.uid + '-cell'" class="ui-grid-cell ng-scope ui-grid-coluiGrid-0006" ng-class="{ 'ui-grid-row-header-cell': col.isRowHeader }" role="gridcell" ui-grid-cell="" id="1540737556446-0-uiGrid-0006-cell">

<div class="ui-grid-cell-contents ng-binding ng-scope">Archana</div>

</div>

XPATH

Orinal xpath. But shouldnt be used. As the numbers in ID might vary.
//div[text()='Archana']//ancestor::div[@id = "1540737556446-0-uiGrid-0006-cell"]


xptah to be used.

//div[text()='Archana']//ancestor::div[contains(@id,'0006')]




Navigate from FirstName to Email:

HTML Text





XPATH:
//div[text()='Archana']//ancestor::div[contains(@id,'0006')]//preceding-sibling::div[contains(@id,'0005')]//div

Here preceding-sibling has been used to refer to the div class which is above the div that we were previously referring to.


Navigate to Gender from FirstName

HTML text




XPATH

we will first refer to 1 the to 2 then 3 and finaly 4.

//div[text()='Archana']//ancestor::div[contains(@id,'0006')]//following-sibling::div[contains(@id,'0007')]//div

Conclusion:
Things to use for xpath

1) Ansestor
2) Following -sibling
3) preceding-sibling
4) Focus on xpath that is not likely to change and proceed from there onwards.
5) Use contains when the attribute is not consistant and changes with HTML dom.
6) Aboid using class. Most of the time its never constant.
7) If ID is present, use ID.


Try catch in Selenium

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.UnhandledAlertException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class TrycatchUnderstand {
    public WebDriver driver = new ChromeDriver();
    @Test
    public void tcu(){
       
            //WebDriver driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
        try{   
            driver.findElement(By.xpath("//input[@type='submit'][@name='proceed']")).click();
            driver.findElement(By.xpath("//a[text()='Home']")).click();
            driver.close();
        }
        catch(UnhandledAlertException uae){
            System.out.println("Alert has ocured: "+uae);
            Alert al = driver.switchTo().alert();
            al.accept();
            driver.switchTo().defaultContent();
        }
        finally{
            driver.findElement(By.xpath("//a[text()='Home']")).click();
            driver.close();
        }
       
    }

}