Hello,
I am doing Module 20 Release Year 2015 part 10 working for LoginTest.
When it opens firefox or chrome, it opens pop up. and I tried using closePopupIfPresent() but it does not work.
The popup screen shot is attached
-----------------------------------------------------------------------------------------------------------------
LoginTest.java
-----------------------------------------------------------------------------------------------------------------
package com.selenium.framework.datadriven.portfolio;
import java.util.Hashtable;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.selenium.framework.datadriven.TestBase;
import com.selenium.framework.datadriven.TestBase;
import com.selenium.framework.datadriven.util.Constants;
import com.selenium.framework.datadriven.util.TestDataProvider;
import com.selenium.framework.datadriven.util.Utility;
import com.selenium.framework.datadriven.util.Xls_Reader;
public class LoginTest extends TestBase {
@Test(dataProviderClass = TestDataProvider.class, dataProvider="PortfolioDataProvider")
public void loginTest(Hashtable<String,String> table) throws InterruptedException{
APPLICATION_LOG.debug("Executing test1");
validateRunmodes("loginTest", Constants.PORTFOLIO_SUITE, table.get(Constants.RUNMODE_COL));
openBrowser(table.get(Constants.BROWSER_COL));
Thread.sleep(5000);
closePopupIfPresent();
Thread.sleep(5000);
navigate("testSiteURL");
click("moneyLink_xpath");
click("myPortfolio_xpath");
}
}
-------------------------------------------------------------------------------------------------------------------
TestBase.java //with closepopup function
------------------------------------------------------------------------------------------------------------------
package com.selenium.framework.datadriven;
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.SkipException;
import com.gargoylesoftware.htmlunit.javascript.host.ApplicationCache;
import com.selenium.framework.datadriven.util.Constants;
import com.selenium.framework.datadriven.util.Utility;
import com.selenium.framework.datadriven.util.Xls_Reader;
public class TestBase {
public static Properties prop;
public static Logger APPLICATION_LOG = Logger.getLogger("devpinoyLogger");
WebDriver driver;
public static void init(){
if(prop == null){
String path=System.getProperty("user.dir")+"\\src\\test\\resources\\project.properties";
prop = new Properties();
try {
FileInputStream fs = new FileInputStream(path);
prop.load(fs);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void validateRunmodes(String testName,String suiteName,String dataRunmode){
APPLICATION_LOG.debug("Validating runmode for " + testName + " in suite " + suiteName);
init();
//suite runmode
boolean suiteRunmode=Utility.isSuiteRunnable(suiteName, new Xls_Reader(prop.getProperty("xlsFileLocation")+"Suite.xlsx"));
boolean testRunmode=Utility.isTestCaseRunnable(testName, new Xls_Reader(prop.getProperty("xlsFileLocation")+ suiteName +".xlsx"));
boolean dataSetRunmode=false;
if(dataRunmode.equals(Constants.RUNMODE_YES))
dataSetRunmode=true;
if(!(suiteRunmode && testRunmode && dataSetRunmode)){
APPLICATION_LOG.debug("Skipping the test "+testName+" inside the suite "+ suiteName);
throw new SkipException("Skipping the test "+testName+" inside the suite "+ suiteName);
}
}
public void closePopupIfPresent(){
Set<String> winIds = driver.getWindowHandles();
System.out.println("Total windows -> "+ winIds.size());
if(winIds.size() == 2){
Iterator<String> iter = winIds.iterator();
String mainWinID = iter.next();
String popupWinID = iter.next();
driver.switchTo().window(popupWinID);
driver.close();
driver.switchTo().window(mainWinID);
}
}
/*******************************Generic functions***************************************/
public void openBrowser(String browserName){
if(browserName.equals("Mozilla")){
driver = new FirefoxDriver();
}else if(browserName.equals("Chrome")){
System.setProperty("webdriver.chrome.driver", prop.getProperty("chromedriverexe"));
driver= new ChromeDriver();
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
public void navigate(String URLKey){
driver.get(prop.getProperty(URLKey));
}
public void click(String identifier){
if(identifier.endsWith("_xpath"))
driver.findElement(By.xpath(identifier)).click();
else if(identifier.endsWith("id"))
driver.findElement(By.id(identifier)).click();
else if(identifier.endsWith("name"))
driver.findElement(By.name(identifier)).click();
}
public void input(String identifier, String data){
}
}