package junitprograms;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
class GoogleTest {
private static WebDriver driver=null;
private static WebDriverWait wait=null;
@BeforeAll
static void setUpBeforeClass() throws Exception {
System.out.println("Executing the @BeforeAll - setUpBeforeClass() ");
//set the chromedriver.exe path
System.setProperty("webdriver.chrome.driver", "D:\\webdriverjars\\executables\\chromedriver_win32\\chromedriver.exe");
//interface refobj=new implementingclass();
driver=new ChromeDriver();
//maximize the window
driver.manage().window().maximize();
//add implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//create object for WebDriverWait class
wait=new WebDriverWait(driver,30);
}
@AfterAll
static void tearDownAfterClass() throws Exception {
System.out.println("Executing the @AfterAll -tearDownAfterClass() ");
driver.close();
}
@BeforeEach
void setUp() throws Exception {
System.out.println("Executing @BeforeEach - setUp() ...");
//open the google.com
driver.get("https://google.com");
wait.until(ExpectedConditions.titleContains("Google"));
Assert.assertEquals("Google", driver.getTitle());
}
@AfterEach
void tearDown() throws Exception {
//clear the cookies
System.out.println("executing the @AfterEach");
driver.manage().deleteAllCookies();
System.out.println("Cleared the cookies in @AfterEach");
}
@Test
void testGoogleLogo() {
System.out.println("Executing the @Test -testGoogleLogo() ");
//identify the Google logo element
WebElement glogo=driver.findElement(By.id("hplogo"));
//fetch the tooltip of the logo
String tp=glogo.getAttribute("title");
System.out.println("glogo tooltip is-->"+tp);
Point p=glogo.getLocation();
System.out.println("glogo x coordinate:"+p.getX()+" y coordinate :"+p.getY());
Dimension d=glogo.getSize();
System.out.println("height of the logo:"+d.getHeight()+" width: "+d.getWidth());
System.out.println("End of the @Test -testGoogleLogo() ");
}
@Test
public void testGoogleSearch() {
System.out.println("Executing the @Test -testGoogleSearch() ");
//type the selenium keyword in search editbox
driver.findElement(By.name("q")).sendKeys("selenium");
//submit on the search editbox
driver.findElement(By.name("q")).submit();
//verify the search results page title
wait.until(ExpectedConditions.titleContains("selenium - Google Search"));
//verify the search results count text is present in the webpage or not.
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("result-stats")));
//fetch the search results count text
String txt=driver.findElement(By.cssSelector("div#result-stats")).getText();
System.out.println("Searc hresults text is-->"+txt);
//String txt="About 4,71,00,000 results (0.52 seconds) ";
//extract the only count from search results count text using split(delimiter) --String[]
String[] str=txt.split(" ");
//str[]=["About","4,71,00,000","results","(0.52","seconds)]
// 0 1 2 3 4
System.out.println("results text is-->"+str[1]);
System.out.println("End of the @Test -testGoogleSearch() ");
}
}
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
class GoogleTest {
private static WebDriver driver=null;
private static WebDriverWait wait=null;
@BeforeAll
static void setUpBeforeClass() throws Exception {
System.out.println("Executing the @BeforeAll - setUpBeforeClass() ");
//set the chromedriver.exe path
System.setProperty("webdriver.chrome.driver", "D:\\webdriverjars\\executables\\chromedriver_win32\\chromedriver.exe");
//interface refobj=new implementingclass();
driver=new ChromeDriver();
//maximize the window
driver.manage().window().maximize();
//add implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//create object for WebDriverWait class
wait=new WebDriverWait(driver,30);
}
@AfterAll
static void tearDownAfterClass() throws Exception {
System.out.println("Executing the @AfterAll -tearDownAfterClass() ");
driver.close();
}
@BeforeEach
void setUp() throws Exception {
System.out.println("Executing @BeforeEach - setUp() ...");
//open the google.com
driver.get("https://google.com");
wait.until(ExpectedConditions.titleContains("Google"));
Assert.assertEquals("Google", driver.getTitle());
}
@AfterEach
void tearDown() throws Exception {
//clear the cookies
System.out.println("executing the @AfterEach");
driver.manage().deleteAllCookies();
System.out.println("Cleared the cookies in @AfterEach");
}
@Test
void testGoogleLogo() {
System.out.println("Executing the @Test -testGoogleLogo() ");
//identify the Google logo element
WebElement glogo=driver.findElement(By.id("hplogo"));
//fetch the tooltip of the logo
String tp=glogo.getAttribute("title");
System.out.println("glogo tooltip is-->"+tp);
Point p=glogo.getLocation();
System.out.println("glogo x coordinate:"+p.getX()+" y coordinate :"+p.getY());
Dimension d=glogo.getSize();
System.out.println("height of the logo:"+d.getHeight()+" width: "+d.getWidth());
System.out.println("End of the @Test -testGoogleLogo() ");
}
@Test
public void testGoogleSearch() {
System.out.println("Executing the @Test -testGoogleSearch() ");
//type the selenium keyword in search editbox
driver.findElement(By.name("q")).sendKeys("selenium");
//submit on the search editbox
driver.findElement(By.name("q")).submit();
//verify the search results page title
wait.until(ExpectedConditions.titleContains("selenium - Google Search"));
//verify the search results count text is present in the webpage or not.
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("result-stats")));
//fetch the search results count text
String txt=driver.findElement(By.cssSelector("div#result-stats")).getText();
System.out.println("Searc hresults text is-->"+txt);
//String txt="About 4,71,00,000 results (0.52 seconds) ";
//extract the only count from search results count text using split(delimiter) --String[]
String[] str=txt.split(" ");
//str[]=["About","4,71,00,000","results","(0.52","seconds)]
// 0 1 2 3 4
System.out.println("results text is-->"+str[1]);
System.out.println("End of the @Test -testGoogleSearch() ");
}
}
========================================================================
HEROKUAPP WebSite LOGIN SCENARIOS
SCENARIO:
1)Open the browser
2)open the url :http://the-internet.herokuapp.com/
3)Verify the Page title and page heading
4)Click on Form Authentication link
5)Verify the login page Addressbar url and page heading
6)Login with valid credentials
7)Verify the secure Area page heading and success message
8)click on Logout button
9)Login with Validusername and invalid password
10)Verify the Error message
11)Login with Invalidusername and valid password
12)Verify the Error message
13)Login with InvalidCrdentials
14)Verify the Error Message
15)close the browser
----------------------------------------------------------------------------------------------------------------
package junitprograms;
import static org.junit.jupiter.api.Assertions.*;
import java.time.Duration;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
class HerokuAppLoginTest {
private static WebDriver driver = null;
private static WebDriverWait wait = null;
@BeforeAll
static void setUpBeforeClass() throws Exception {
// set the chromedriver.exe file path
System.setProperty("webdriver.chrome.driver",
"D:\\webdriverjars\\executables\\chrome971\\chromedriver_win32\\chromedriver.exe");
// interface refvar=new implementedclass();
driver = new ChromeDriver();
// maximize the window
driver.manage().window().maximize();
// add implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(10000));
// open the url in browser
driver.get("http://the-internet.herokuapp.com/");
// create Object for WebDriverWait class
wait = new WebDriverWait(driver, Duration.ofMillis(30000));
wait.until(ExpectedConditions.titleContains("The Internet"));
// wait for the home page heading css=tagname.classvalue
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("h1.heading")));
// fetch the header text
String headertxt = driver.findElement(By.cssSelector("h1.heading")).getText();
// assertEquals()
Assertions.assertEquals(headertxt, "Welcome to the-internet");
// assertTrue()
Assertions.assertTrue(driver.getPageSource().contains(headertxt));
// click on Form Authentication
driver.findElement(By.linkText("Form Authentication")).click();
}
@AfterAll
static void tearDownAfterClass() throws Exception {
if (driver != null) {
driver.quit();
}
}
@BeforeEach
void setUp() throws Exception {
// wait for the url
wait.until(ExpectedConditions.urlContains("http://the-internet.herokuapp.com/login"));
// wait for the page heading
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@id='content']/div/h2")));
// fetch teh header text Loginpage
String loginpgheadertxt = driver.findElement(By.cssSelector("div#content>div>h2")).getText();
// using assertEquals()
Assertions.assertEquals("Login Page", loginpgheadertxt);
// using assertTrue()
Assertions.assertTrue(driver.getPageSource().contains(loginpgheadertxt));
}
@AfterEach
void tearDown() throws Exception {
// clear the cookies
driver.manage().deleteAllCookies();
}
@Test
void testWithValidCredentials() {
System.out.println("started executing the testWithValidCredentials()....");
doLogin("tomsmith", "SuperSecretPassword!");
// driver.findElement(By.xpath("//button[@type='submit']")).submit();
// wait for the http://the-internet.herokuapp.com/secure
wait.until(ExpectedConditions.urlToBe("http://the-internet.herokuapp.com/secure"));
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@id='flash-messages']/div")));
// waiting for the secureArea headertext
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#content>div>h2")));
// assertTrue
Assertions.assertTrue(driver.getPageSource().contains("Secure Area"));
// click on logout button
driver.findElement(By.cssSelector("a.button.secondary.radius")).click();
}
@Test
public void testValidUserNameInvalidPassword() {
System.out.println("started executing the testValidUserNameInvalidPassword()....");
doLogin("tomsmith", "gfads576a5s");
verifyErrorMessage("Your password is invalid!");
}
@Test
public void testInvalidUserNameAndValidPassword() {
System.out.println("started executing the testInvalidUserNameAndValidPassword()....");
doLogin("ksjdfhksjdf", "SuperSecretPassword!");
verifyErrorMessage("Your username is invalid!");
}
@Test
public void testWithInvalidCrdentials() {
System.out.println("started executing the testWithInvalidCrdentials()....");
doLogin("ksjdfhksjdf", "73642874382!");
verifyErrorMessage("Your username is invalid!");
}
/**
* this method is used to do a login using usernamd and pasword
* @param uname
* @param pwd
*/
private void doLogin(String uname, String pwd) {
// type the username value in usernameTextbox
driver.findElement(By.id("username")).sendKeys(uname);
// type the password in passwordtextbox
driver.findElement(By.name("password")).sendKeys(pwd);
// click on Login button
driver.findElement(By.className("radius")).click();
}
private void verifyErrorMessage(String msg) {
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.flash.error")));
Assertions.assertTrue(driver.getPageSource().contains(msg));
}
}
================================================================
ALERTS PROGRAM ON HEROKUAPP WEBSITE:
Scenario:
1)Open the browser
2)open the url :http://the-internet.herokuapp.com/
3)Verify the Page title and page heading
4)Click on JavaScript Alerts link
5)Verify the JavaScript Alerts Addressbar url and page heading
6)Click for Js Alert button
7)Handle Simple alert
8)Verify the Alert Result Text
9)Click for JS Confirm button
10)Handle Confirmation dialogbox
11)click on confirmation dialogbox Ok button
12)Verify the Alert Result Text for Ok button
13)Click for JS Confirm button again
14)Handle Confirmation dialogbox
15)click on confirmation dialogbox Cancel button
16)Verify the Alert Result Text for cancel
17)Click for JS Prompt button
18)Handle Prompt dialogbox
19)type the value in editbox
20)Click on Prompt Ok button
21)Verify the Prompt Result Text for Ok button
22)Click for JS Prompt button again
23)Handle Prompt dialogbox
24)type the value in editbox
25)click on prompt dialogbox Cancel button
26)Verify the prompt Result Text for cancel
27)Close the browser
---------------------------------------------------------------------------------------
package junitprograms;
import static org.junit.jupiter.api.Assertions.*;
import java.time.Duration;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
class HerokuAppJSAlertsTest {
private static WebDriver driver = null;
private static WebDriverWait wait = null;
@BeforeAll
static void setUpBeforeClass() throws Exception {
// set the chromedriver.exe file path
System.setProperty("webdriver.chrome.driver",
"D:\\webdriverjars\\executables\\chrome971\\chromedriver_win32\\chromedriver.exe");
// interface refvar=new implementedclass();
driver = new ChromeDriver();
// maximize the window
driver.manage().window().maximize();
// add implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(10000));
// open the url in browser
driver.get("http://the-internet.herokuapp.com/");
// create Object for WebDriverWait class
wait = new WebDriverWait(driver, Duration.ofMillis(30000));
wait.until(ExpectedConditions.titleContains("The Internet"));
// wait for the home page heading css=tagname.classvalue
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("h1.heading")));
// fetch the header text
String headertxt = driver.findElement(By.cssSelector("h1.heading")).getText();
// assertEquals()
Assertions.assertEquals(headertxt, "Welcome to the-internet");
// assertTrue()
Assertions.assertTrue(driver.getPageSource().contains(headertxt));
// click on JavaScript Alerts
driver.findElement(By.linkText("JavaScript Alerts")).click();
}
@AfterAll
static void tearDownAfterClass() throws Exception {
if (driver != null) {
driver.quit();
}
}
@BeforeEach
void setUp() throws Exception {
// wait for the url
wait.until(ExpectedConditions.urlContains("http://the-internet.herokuapp.com/javascript_alerts"));
// wait for the page heading
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='example']/h3")));
// fetch teh header text Loginpage
String jsheadertxt = driver.findElement(By.cssSelector("div.example>h3")).getText();
// using assertEquals()
Assertions.assertEquals("JavaScript Alerts", jsheadertxt);
// using assertTrue()
Assertions.assertTrue(driver.getPageSource().contains(jsheadertxt));
}
@AfterEach
void tearDown() throws Exception {
// clear the cookies
driver.manage().deleteAllCookies();
}
@Test
void testSimpleAlert() {
System.out.println("started executing the testSimpleAlert()....");
// click on 'Click for JS Alert'
driver.findElement(By.xpath("//button[text()='Click for JS Alert']")).click();
// switch the focus to alert dialogbox
Alert alt = driver.switchTo().alert();
System.out.println("alert text is:" + alt.getText());
// close the simple alert
alt.accept();
verifyResultMessage("You successfully clicked an alert");
}
@Test
public void testConfirmationDialogbox() {
System.out.println("started executing the testConfirmationDialogbox()....");
// click on 'Click for JS Confirm'
driver.findElement(By.xpath("//button[text()='Click for JS Confirm']")).click();
// switch the focus to alert dialogbox
Alert cnf = driver.switchTo().alert();
System.out.println("alert text is:" + cnf.getText());
// click ok buttin in confirmation dialog
cnf.accept();
verifyResultMessage("You clicked: Ok");
// click on 'Click for JS Confirm'
driver.findElement(By.xpath("//button[text()='Click for JS Confirm']")).click();
// switch the focus to alert dialogbox
Alert cnf1 = driver.switchTo().alert();
System.out.println("alert text is:" + cnf1.getText());
// click cancel button in confirmation dialogt
cnf1.dismiss();
verifyResultMessage("You clicked: Cancel");
}
@Test
public void testPromptDialogbox() {
System.out.println("started executing the testPromptDialogbox()....");
// click on 'Click for JS Prompt'
driver.findElement(By.xpath("//button[text()='Click for JS Prompt']")).click();
// switch the focus to prompt alert dialogbox
Alert prmpt = driver.switchTo().alert();
System.out.println("alert text is:" + prmpt.getText());
// type the value in prompt editbox
prmpt.sendKeys("Selenium");
// click ok buttin in prompt dialog
prmpt.accept();
verifyResultMessage("You entered: Selenium");
// click on 'Click for JS Prompt'
driver.findElement(By.xpath("//button[text()='Click for JS Prompt']")).click();
// switch the focus to alert dialogbox
Alert prmpt1 = driver.switchTo().alert();
System.out.println("alert text is:" + prmpt1.getText());
prmpt1.sendKeys("webdriver");
// click cancel button in prompt dialogt
prmpt1.dismiss();
verifyResultMessage("You entered: null");
}
private void verifyResultMessage(String expmsg) {
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("p#result")));
String actMsg = driver.findElement(By.cssSelector("p#result")).getText();
System.out.println("alert result mesage is:" + actMsg);
Assertions.assertEquals(expmsg, actMsg);
}
}
=========================================================================
FRAMES PROGRAM ON HEROKUAPP WEBSITE:
SCENARIO:
1)Open the browser
2)open the url :http://the-internet.herokuapp.com/
3)Verify the Page title and page heading
4)Click on Frames link
5)Verify the Frames Addressbar url and page heading
6)Click on Nested Frames Link
7)Verify the Nested Frames page heading and URL
8)Fetch all the frames in the page
9)Switch to to frame and nested middle frame
10)Get the MIDDLE text and assert it
11)Navigate back to original position from frame
12Close the browser
-------------------------------------------------------------------------------------------------
package junitprograms;
import static org.junit.jupiter.api.Assertions.*;
import java.time.Duration;
import java.util.List;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchFrameException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
class HerokuAppFrameTest {
private static WebDriver driver = null;
private static WebDriverWait wait = null;
@BeforeAll
static void setUpBeforeClass() throws Exception {
// set the chromedriver.exe file path
System.setProperty("webdriver.chrome.driver",
"D:\\webdriverjars\\executables\\chrome971\\chromedriver_win32\\chromedriver.exe");
// interface refvar=new implementedclass();
driver = new ChromeDriver();
// maximize the window
driver.manage().window().maximize();
// add implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(10000));
// open the url in browser
driver.get("http://the-internet.herokuapp.com/");
// create Object for WebDriverWait class
wait = new WebDriverWait(driver, Duration.ofMillis(30000));
wait.until(ExpectedConditions.titleContains("The Internet"));
// wait for the home page heading css=tagname.classvalue
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("h1.heading")));
// fetch the header text
String headertxt = driver.findElement(By.cssSelector("h1.heading")).getText();
// assertEquals()
Assertions.assertEquals(headertxt, "Welcome to the-internet");
// assertTrue()
Assertions.assertTrue(driver.getPageSource().contains(headertxt));
// click on Frames l
driver.findElement(By.linkText("Frames")).click();
// wait for the url
wait.until(ExpectedConditions.urlContains("http://the-internet.herokuapp.com/frames"));
// wait for the page heading
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='example']/h3")));
// fetch teh header text Loginpage
String frmheadertxt = driver.findElement(By.cssSelector("div.example>h3")).getText();
// using assertEquals()
Assertions.assertEquals("Frames", frmheadertxt);
// using assertTrue()
Assertions.assertTrue(driver.getPageSource().contains(frmheadertxt));
}
@AfterAll
static void tearDownAfterClass() throws Exception {
if (driver != null) {
driver.quit();
}
}
@Test
void testFrames() {
System.out.println("started executing the testFrames()....");
// click on 'Nested Frames'
driver.findElement(By.partialLinkText("Nested Frames")).click();
//wait for the next page title:http://the-internet.herokuapp.com/nested_frames
wait.until(ExpectedConditions.urlToBe("http://the-internet.herokuapp.com/nested_frames"));
//fetch total number of frames
List<WebElement>framList=driver.findElements(By.tagName("frame"));
System.out.println("Number of frames in the page :"+framList.size());
/*//switch to top frame first then switch to child frame
switchToFrame("frame-top");
//fetch number of frames inside top frame
List<WebElement>topframList=driver.findElements(By.tagName("frame"));
System.out.println("Number of frames in the top frame page :"+topframList.size());
//switch to middle frame
switchToFrame("frame-middle");*/
switchToFrame("frame-top", "frame-middle");
//fetch the MIDDLE text
String txt=driver.findElement(By.id("content")).getText();
System.out.println("middle frame text is:"+txt);
Assertions.assertEquals("MIDDLE", txt);
}
/**
* This method will switch to parent frame then to child frame
* @param ParentFrame
* @param ChildFrame
*/
private void switchToFrame(String ParentFrame, String ChildFrame) {
try {
driver.switchTo().frame(ParentFrame).switchTo().frame(ChildFrame);
System.out.println("Navigated to innerframe with id " + ChildFrame
+ "which is present on frame with id" + ParentFrame);
} catch (NoSuchFrameException e) {
System.out.println("Unable to locate frame with id " + ParentFrame
+ " or " + ChildFrame + e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to navigate to innerframe with id "
+ ChildFrame + "which is present on frame with id"
+ ParentFrame + e.getStackTrace());
}
}
/**
* this method will return from frame to original position
*/
private void switchtoDefaultFrame() {
try {
driver.switchTo().defaultContent();
System.out.println("Navigated back to webpage from frame");
} catch (Exception e) {
System.out
.println("unable to navigate back to main webpage from frame"
+ e.getStackTrace());
}
}
/**
* this method switch to given fame locator
* @param frame
*/
public void switchToFrame(String frame) {
try {
driver.switchTo().frame(frame);
System.out.println("Navigated to frame with name " + frame);
} catch (NoSuchFrameException e) {
System.out.println("Unable to locate frame with id " + frame
+ e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to navigate to frame with id " + frame
+ e.getStackTrace());
}
}
}
=================================================================INTERNAL FRAMES PROGRAM
1)open http://jqueryui.com/
2)verify the page title.--jQuery UI
3)click on Autocomplete link under widgets section.
4)verify the page title.--Autocomplete | jQuery UI
5)switch to iframe
6)type the tags value inn tags editbox
7)navigate back to previoius page
8)verify the page title
9)click on Accordion link.
10)verify the page title.--Accordion | jQuery UI
2)verify the page title.--jQuery UI
3)click on Autocomplete link under widgets section.
4)verify the page title.--Autocomplete | jQuery UI
5)switch to iframe
6)type the tags value inn tags editbox
7)navigate back to previoius page
8)verify the page title
9)click on Accordion link.
10)verify the page title.--Accordion | jQuery UI
--------------------------------------------------------------------------------
package junitprograms;
import static org.junit.jupiter.api.Assertions.*;
import java.time.Duration;
import java.util.List;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchFrameException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
class JqueryIFrameTest {
private static WebDriver driver = null;
private static WebDriverWait wait = null;
@BeforeAll
static void setUpBeforeClass() throws Exception {
// set the chromedriver.exe file path
System.setProperty("webdriver.chrome.driver",
"D:\\webdriverjars\\executables\\chrome971\\chromedriver_win32\\chromedriver.exe");
// interface refvar=new implementedclass();
driver = new ChromeDriver();
// maximize the window
driver.manage().window().maximize();
// add implicit wait
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(10000));
// open the url in browser
driver.get("https://jqueryui.com/");
// create Object for WebDriverWait class
wait = new WebDriverWait(driver, Duration.ofMillis(30000));
wait.until(ExpectedConditions.titleContains("jQuery UI"));
// click on AutoComplete link
driver.findElement(By.linkText("Autocomplete")).click();
// wait for the url
wait.until(ExpectedConditions.urlContains("https://jqueryui.com/autocomplete/"));
//switch to iframe
}
@AfterAll
static void tearDownAfterClass() throws Exception {
if (driver != null) {
driver.quit();
}
}
@Test
void testIFrames() {
System.out.println("started executing the testIFrames()....");
//fetch total number of frames
List<WebElement>iframList=driver.findElements(By.tagName("iframe"));
System.out.println("Number of iframes in the page :"+iframList.size());
WebElement ifrmele=driver.findElement(By.className("demo-frame"));
//switch to iframe element
switchToFrame(ifrmele);
//type the value tags editbox
driver.findElement(By.id("tags")).sendKeys("webdriver");
//switch back to jqueryui home page
driver.navigate().back();
wait.until(ExpectedConditions.titleIs("jQuery UI"));
}
/**
* This method will switch to parent frame then to child frame
* @param ParentFrame
* @param ChildFrame
*/
private void switchToFrame(String ParentFrame, String ChildFrame) {
try {
driver.switchTo().frame(ParentFrame).switchTo().frame(ChildFrame);
System.out.println("Navigated to innerframe with id " + ChildFrame
+ "which is present on frame with id" + ParentFrame);
} catch (NoSuchFrameException e) {
System.out.println("Unable to locate frame with id " + ParentFrame
+ " or " + ChildFrame + e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to navigate to innerframe with id "
+ ChildFrame + "which is present on frame with id"
+ ParentFrame + e.getStackTrace());
}
}
/**
* this method will return from frame to original position
*/
private void switchtoDefaultFrame() {
try {
driver.switchTo().defaultContent();
System.out.println("Navigated back to webpage from frame");
} catch (Exception e) {
System.out
.println("unable to navigate back to main webpage from frame"
+ e.getStackTrace());
}
}
/**
* this method switch to given fame locator
* @param frame
*/
public void switchToFrame(String frame) {
try {
driver.switchTo().frame(frame);
System.out.println("Navigated to frame with name " + frame);
} catch (NoSuchFrameException e) {
System.out.println("Unable to locate frame with id " + frame
+ e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to navigate to frame with id " + frame
+ e.getStackTrace());
}
}
public void switchToFrame(WebElement frameElement) {
try {
if (frameElement.isDisplayed()) {
driver.switchTo().frame(frameElement);
System.out.println("Navigated to frame with element "+ frameElement);
} else {
System.out.println("Unable to navigate to frame with element "+ frameElement);
}
} catch (NoSuchFrameException e) {
System.out.println("Unable to locate frame with element " + frameElement + e.getStackTrace());
} catch (StaleElementReferenceException e) {
System.out.println("Element with " + frameElement + "is not attached to the page document" + e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to navigate to frame with element " + frameElement + e.getStackTrace());
}
}
}
=================================================================================================
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.