Hi Ashish,
With the referenc of your tutorial vidoes I am creating POM with page factory framework. In my case, When user logged in a pop up/alert came but I am unable to handle it.I have tried both code for alert and pop up but not got the success. Please help me on that. I am stuck here since1 week.
Thanks in advance.
Below is my code:-
My Test Class Code:-
package testcases;
import java.util.Hashtable;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.surf.base.pages.SurfPage;
import com.surf.constants.Constants;
import com.surf.pages.normal.LaunchPage;
import com.surf.pages.normal.SubmitUserNameAndPasswordPage;
import com.surf.session.SurfTestSession;
import com.surf.util.DataUtil;
import com.surf.util.XLS_Reader;
public class LoginTest {
SurfTestSession surftestsession;
String testCaseName="LoginTest";
String pathofdataexcel=System.getProperty("user.dir")+"\\src\\main\\resources\\Data.xlsx";
XLS_Reader xls_reader=new XLS_Reader(pathofdataexcel);
//1-driver object
//1-instance of report object
//1 -SurfTestSession(Inside SurfTestSession we will manage driver report)
//1- TestContext
// Session has extent reports and extent test
//no declaration of reports in tests
//failure-critical or non critical-reported in extent reports
//screenshots in the report
@BeforeMethod
public void init() {
surftestsession =new SurfTestSession();//new browser,new driver
surftestsession.init(testCaseName);
}
@AfterMethod
public void quit() {
surftestsession.generateReport();
}
@DataProvider
public Object[][] getData(){
return DataUtil.getData(testCaseName, xls_reader);
}
@Test(dataProvider = "getData")
public void loginTest(Hashtable<String,String>data) {
surftestsession.log(data.toString());
if(data.get("Runmode").equals("N")) {
//Skip in Extent Reports
surftestsession.skipTest("Skipping The Test Case As Runmode Was Set As No");
//Skip the test case in TestNG
throw new SkipException("Skipping The Test Case As Runmode Was Set As No");
}
String username=data.get("UserName");
String password=data.get("Password");
String userNameAndPasswordValid=data.get("UserNameAndPasswordValid");
//WebConnector is initialized
//store surftestsession in testcontext
SurfPage surfpage=new LaunchPage().//store session in testcontext,object of LaunchPage
openBrowser("Chrome").
goToLoginPage().
validator(true).
validateLoginPageOfSurfAccounts(Constants.Login_PAGE_LOGO_LOCATOR).// return LoginPage,stop if validation fails
//return LoginPage,don't stop if validation fails
goToSubmitUserNameAndPasswordPage().submitUserNameAndPassword(username,password).goToIntermediateReleaseMonthAndYearPage().validateOptionsInIntermediateReleaseMonthAndYearPopUp();
/*
if(surfpage instanceof SubmitUserNameAndPasswordPage &userNameAndPasswordValid.equals("Y")) {
//failure
surfpage.validator(true).fail("Could Enter Valid Username And Password "+userNameAndPasswordValid);
}else {
//failure
surfpage.validator(true).fail("Could Enter Valid Username And Password "+userNameAndPasswordValid);
}
*/
//validation-if my username is correct - we should be on the password page
//validation-if my username is incorrect - we should be on the password page
//surftestsession.end();//fails the test and ends the session
}
}
My Page Class Code :-
package com.surf.pages.normal;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindAll;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.FindBys;
import com.surf.base.pages.SurfBasePage;
import com.surf.base.pages.SurfPage;
import com.surf.constants.Constants;
public class IntermediateReleaseMonthAndYearPage extends SurfBasePage {
//public static void main(String[] args) {
// System.setProperty("webdriver.chrome.driver","D:\\Selenium Supporting Files\\Browsers\\Chrome_Latest\\chromedriver.exe");
// WebDriver driver=new ChromeDriver();
// driver.switchTo().alert();
//}
//FindAll- satisfies each one
//FindBys-box-element inside element
/*
@FindBys({
@FindBy(xpath="//div[@id='divPermissiontext']/div[2]"),
@FindBy(tagName="input")
})
*/
@FindAll({
@FindBy(id=Constants.optionsinintermediatereleasemonthyearpopup)
})
public List<WebElement> optionsinintermediatereleasemonthandyearpopup;
public SurfPage validateOptionsInIntermediateReleaseMonthAndYearPopUp() {
System.setProperty("webdriver.chrome.driver","D:\\Selenium Supporting Files\\Browsers\\Chrome_Latest\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
String mainwindow=driver.getWindowHandle();
System.out.println("The ID Of Main Window Is "+mainwindow);
Set<String> childwindows=driver.getWindowHandles();
System.out.println("Total no. of child windows are present "+childwindows.size());
Iterator<String>childwindow=childwindows.iterator();
while(childwindow.hasNext())
System.out.println(childwindow.next());
//driver.switchTo().alert();
System.out.println("No. of options available in intermediate release month and year pop up "+optionsinintermediatereleasemonthandyearpopup.size());
List<String>textofoptionsofintermediatereleasemonthandyearpopup=Arrays.asList(Constants.optionsinintermediatereleasemonthyearpopup);
for(int i=0;i<optionsinintermediatereleasemonthandyearpopup.size();i++) {
System.out.println("Text Of" +i +"option is"+textofoptionsofintermediatereleasemonthandyearpopup.get(i));
//if(!textofoptionsofintermediatereleasemonthandyearpopup.contains(textofoptionsofintermediatereleasemonthandyearpopup.get(i)))
//validator(false).fail("Options of intermediate release month and year pop up not found "+textofoptionsofintermediatereleasemonthandyearpopup.get(i));
}
return this;
}
}
My Application :-
Instructor
Ashish Thakur Replied on 10/11/2022
This is not a popup
Popup is a completely different window
This can be:
1) Part of same page- In this case you can directly manage it
2) A frame. You can switch to the frame and manage it
okay thanks.