Monday, August 24, 2020

selenium webdriver tutorials

  

First Selenium Program

 

1.eclipse download

 

2.File->New ->Java Project àProjectname àFinish

3.selenium software download for java

https://www.selenium.dev/downloads/

 


 

 4.go to eclipse project right click the library folder

 


 

5.build path-àconfig build path


6.Add external jars (selenium library files add)


 

7.Apply and close

8.coding type

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class s {

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

 

              System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

              WebDriver driver = new ChromeDriver();

        driver.get("https://www.google.com");

       }

 

}

  

2.Locators Check

 

Name  Locator

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class s {

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

 

              System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

              WebDriver driver = new ChromeDriver();

        driver.get("https://www.google.com");

        driver.findElement(By.name("q")).sendKeys("tech");

        driver.findElement(By.name("btnK")).submit();

       }

 

}

 

 

Id Locator

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class s {

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

 

              System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

              WebDriver driver = new ChromeDriver();

        driver.get("https://www.facebook.com");

        driver.findElement(By.id("email")).sendKeys("tech");

       }

 

}

 

Xpath Locator

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class s {

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

 

              System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

              WebDriver driver = new ChromeDriver();

        driver.get("https://www.facebook.com");

        driver.findElement(By.xpath("//*[@id=’email’]")).sendKeys("tech");

       }

 

}


 

Link Text

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class s {

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

 

              System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

              WebDriver driver = new ChromeDriver();

        driver.get("https://www.facebook.com");

        driver.findElement(By.linkText("Forgotten account?")).click();

      

       }

 

}

 

Partial Link Text

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class s {

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

 

              System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

              WebDriver driver = new ChromeDriver();

        driver.get("https://www.facebook.com");

        driver.findElement(By.partialLinkText("Forgotten")).click();

      

       }

 

}

 

CssSelector

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class s {

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

 

              System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

              WebDriver driver = new ChromeDriver();

        driver.get("https://www.facebook.com");

        driver.findElement(By.cssSelector("#u_0_m")).sendKeys("hai");

      

       }

 

}

 

Check Box and Radio Button

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class s {

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

 

              System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

              WebDriver driver = new ChromeDriver();

        driver.get("http://demo.guru99.com/test/radio.html");

        driver.findElement(By.id("vfb-7-1")).click();

        driver.findElement(By.id("vfb-6-0")).click();

       

      

       }

 

}

 

DropDown

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.Select;

 

public class s {

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

 

              System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

              WebDriver driver = new ChromeDriver();

        driver.get("http://demo.guru99.com/test/newtours/register.php");

        Select s = new Select(driver.findElement(By.name("country")));

        s.selectByVisibleText("INDIA");

      

       }

 

}

 

Webtable

Html file

 

<html>

<body>

<table border=”1”>

<tbody>

<tr>

<td>

First

</td>

<td>

Second

</td>

</tr>

</tbody>

</table>

 


 

Code

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.Select;

 

public class s {

 

       public static void main(String[] args) {

              // TODO Auto-generated method stub

 

              System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

              WebDriver driver = new ChromeDriver();

        driver.get("file:///C:/Users/ADMIN/Desktop/webtable.html");

      String adriver.findElement(By.xpath("/html/body/table/tbody/tr/td[2]")).getText();

      System.out.println(a);

       }

 

}

 

 

Installing TestNG in Eclipse

Go to Help Install new Software

 


 

 

Step 2

Paste the link http://dl.bintray.com/testng-team/testng-eclipse-release/


 

Click add button install testing

 

First TestNG project

 

Step 1

File->New ->other->TestNG->TestNG class


 

Click next

 


Create the testing project click finish

Right click the project folderàbuild path-àconfigurebuild path-àadd Library


Add TestNG library

 

 

Program

package p1;

 

import org.testng.annotations.Test;

import org.openqa.selenium.*;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;

import org.testng.annotations.*;

 

public class testngexample {

  @Test

  public void f() {

         System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

              WebDriver driver = new ChromeDriver();

      driver.get("https://www.google.com");

 

        

  }

}

 

Run


 

Getting html report

 

1.Right click the project folder

2.Click refresh

3.test-output folder(click index.html) get the html report

 

 

Annotation using TestNG

package p1;

 

import org.testng.annotations.Test;

import org.testng.annotations.BeforeTest;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.AfterTest;

 

public class NewTest {

 

              WebDriver driver ;

 

       @Test

  public void f() {

             

              driver= new ChromeDriver();

                 driver.get("https://www.google.com");

  }

  @BeforeTest

  public void beforeTest() {

         System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

             

  }

 

  @AfterTest

  public void afterTest() {

         driver.close();

  }

 

}

 

TestPrioirty

 

package p1;

 

import org.testng.annotations.Test;

import org.testng.annotations.BeforeTest;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.AfterTest;

 

public class NewTest {

 

              WebDriver driver ;

 

        @Test(priority = 1)

 

  public void f() {

             

              driver= new ChromeDriver();

                 driver.get("https://www.google.com");

  }

    @Test(priority = 0)

 

  public void beforeTest() {

         System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

             

  }

 

    @Test(priority = 2)

 

  public void afterTest() {

         driver.close();

  }

 

}

 

MultiSessions

 

 

package p1;

 

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;

 

public class NewTest123 {

      

 

  @Test

  public void f() {

         System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

         WebDriver drivernew ChromeDriver();

                 driver.get("https://www.google.com");

  }

 

  @Test

  public void f1() {

         System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

         WebDriver drivernew ChromeDriver();

                 driver.get("https://www.facebook.com");

  }

 

 

}

Now create TestNG.xml


 

 


 

TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="Suite">

  <test thread-count="5" name="Test">

    <classes>

      <class name="p1.NewTest123"/>

      <class name="p1.example"/>

    </classes>

  </test> <!-- Test -->

</suite> <!-- Suite -->

 

 

 

Run the program

 

 


 

 

Multiple Test Suites

 

Package 1(package p1)

package p1;

 

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;

 

public class NewTest123 {

      

 

  @Test

  public void f() {

         System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

         WebDriver drivernew ChromeDriver();

                 driver.get("https://www.google.com");

  }

 

 

 

}

 

 

Package 2(package p1)

package p2;

 

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;

 

public class NewTest {

  @Test

  public void f() {

         System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

         WebDriver drivernew ChromeDriver();

                 driver.get("https://www.facebook.com");

  }

}

 

TestNG.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="Suite">

  <test thread-count="5" name="Test">

    <classes>

      <class name="p1.NewTest123"/>

      <class name="p2.NewTest"/>

    </classes>

  </test> <!-- Test -->

</suite> <!-- Suite -->

 

TestNG Report Generation

 

Emailable-report.html

 


 



 

2.index.html

 


 


 

3.Report Class

 

Example

 

package p2;

 

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Reporter;

import org.testng.annotations.Test;

 

public class NewTest {

  @Test

  public void f() {

         System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

         Reporter.log("the browser open");

 

         WebDriver drivernew ChromeDriver();

         Reporter.log("the browser type");

                 driver.get("https://www.facebook.com");

  }

}

 

Run the TestNG.xml file and refresh the folder

Open the emailable.html

 


 

Then we get the output

 

Take ScreenShot

Add commons-io-2.7.jar file to build path then

 

program

 

package p2;

 

import java.io.File;

 

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Reporter;

import org.testng.annotations.Test;

 

public class NewTest {

  @Test

  public void f() {

         System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");

        

 

         WebDriver drivernew ChromeDriver();

      

                 driver.get("https://www.facebook.com");

                

                 TakesScreenshot scrShot =((TakesScreenshot)driver);

           File src=scrShot.getScreenshotAs(OutputType.FILE);

           File DestFile=new File("D://");

           FileUtils.copyFile(srcDestFile);

  }

}

 

 

 

 

 

No comments:

Post a Comment

Python Technical Interview Questions

 1)first non repeating character Malayalam y programming p str = "MalayalaM" a = '\0' for c in str:     if str.index(c) ==...