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();
        }
       
    }

}

Working with window handle popup in Selenium

import java.util.Iterator;
import java.util.Set;

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

public class Popupcheck {
    @Test
    public void pophdfc() throws Exception{
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.hdfcbank.com/"); //GO TO WEBSITE
        String homewindow = driver.getWindowHandle();
        driver.findElement(By.xpath("//a[text()='Login']")).click(); //ANOTHER WINDOW OPENS
        Set<String> windows = driver.getWindowHandles();
       
        Iterator ite = windows.iterator();
        String windowids;
        while(ite.hasNext()){
            windowids = ite.next().toString();
            if(!windowids.equals(homewindow)){
                driver.switchTo().window(windowids);
                driver.close();
            }           
        }
        driver.switchTo().window(homewindow);
        driver.close();
    }
}

Dealing with JavaScript PopUP

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

//Dealing with JavaScript Popup
public class LearnSel {
    @Test
    public void case1(){
        WebDriver driver = new ChromeDriver();
        driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
        driver.manage().window().maximize();
        driver.findElement(By.name("proceed")).click();
        Alert alert = driver.switchTo().alert();
        String al = alert.getText();
        System.out.println(al);
        alert.accept();
        driver.switchTo().defaultContent();
        driver.close();
       
    }

}






What is TestNG


TestNG is an automation testing framework where NG is Next Generation.
By using TestNG one can easily generate a report in HTML format, and can analyse the number of test cases that have passed , failed and skipped. It basically gives a overview of the test.
TestNG , like JUnit uses the annotations. i.e @ symbol
                                 
                     

Why to use TestNG with Selenium?

1. Selenium tests do produce results, but they are not in a expected format for the test results. But by using TestNG one can generate these much expected test results.
·         
     2. Generating the report in a proper format includes  number of test cases runs, the number of test cases passed, the number of test cases failed, and the number of test cases skipped.
·      
           3. Multiple test cases can be grouped easily by converting them into.xml file. One can also prioritise which test case should be executed first, middle or last.
·         
          4. The same test case can be executed multiple times without loops just by using keyword called 'invocation count.'
·         Using  TestNG one can achieve cross browser testing.
·         The testing framework can be easily integrated with build tools like Maven, Ant etc.


TestNG Annotations

@BeforeSuite: The annotated method will be run before all tests in this suite have run.

@AfterSuite: The annotated method will be run after all tests in this suite have run.

@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.

@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.

@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.

@BeforeMethod: The annotated method will be run before each test method.

@AfterMethod: The annotated method will be run after each test method.

@Test: The annotated method is a part of a test case

@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.

@AfterClass: The annotated method will be run after all the test methods in the current class have been run.

What is Selenium


·         Selenium is a portable software-testing framework for web applications. Selenium provides a playback (formerly also recording) tool for authoring tests without the need to learn a test scripting language (Selenium IDE).
·         It also provides a test domain-specific language (Selenese) to write tests in a number of popular programming languages, including C#, Groovy, Java, Perl, PHP, Python, Ruby and Scala. The tests can then run against almost all web browsers.
·         Selenium deploys on Windows, Linux, and macOS platforms. It is open-source software, released under the Apache 2.0 license: web developers can download and use it without charge.

                                                                                       
                                         

Stable release   3.14.0 / August 17, 2018
Versions:
Selenium IDE
Selenium IDE is a complete integrated development environment (IDE) for Selenium tests. It is implemented as a Firefox Add-On and available on Chrome Store recently, and allows recording, editing, and debugging tests. It was previously known as Selenium Recorder.
Scripts can be automatically recorded and edited manually providing auto completion support and the ability to move commands around quickly. Scripts are recorded in Selenese, a special test scripting language for Selenium.
Selenium client API
As an alternative to writing tests in Selenese, tests can also be written in various programming languages. These tests then communicate with Selenium by calling methods in the Selenium Client API. Selenium currently provides client APIs for Java, C#, Ruby, JavaScript and Python.
Selenium WebDriver
Selenium Web Driver is the successor to Selenium RC. Selenium WebDriver accepts commands (sent in Selenese, or via a Client API) and sends them to a browser. This is implemented through a browser-specific browser driver, which sends commands to a browser and retrieves results. Most browser drivers actually launch and access a browser application (such as Firefox, Chrome, Internet Explorer, Safari, or Microsoft Edge); there is also an HtmlUnit browser driver, which simulates a browser using the headless browser HtmlUnit.
Selenium Remote Control
Selenium Remote Control (RC) is a server, written in Java, that accepts commands for the browser via HTTP. RC makes it possible to write automated tests for a web application in any programming language, which allows for better integration of Selenium in existing unit test frameworks. To make writing tests easier, Selenium project currently provides client drivers for PHP, Python, Ruby, .NET, Perl and Java. The Java driver can also be used with JavaScript (via the Rhino engine). An instance of selenium RC server is needed to launch html test case - which means that the port should be different for each parallel run.[citation needed] However, for Java/PHP test case only one Selenium RC instance needs to be running continuously.

Selenium Grid
Selenium Grid is a server that allows tests to use web browser instances running on remote machines. With Selenium Grid, one server acts as the hub. Tests contact the hub to obtain access to browser instances. The hub has a list of servers that provide access to browser instances (WebDriver nodes), and lets tests use these instances. Selenium Grid allows running tests in parallel on multiple machines, and to manage different browser versions and browser configurations centrally (instead of in each individual test).

In other words, Selenium automates browsers. That's it!
Reference: 
To download Selenium, please visit seleniumhq.org/