How to change download path during runtime using Selenium with chrome driver - java

I have 20 pages and each page have 2 testcases and each testcase download a number of files. I want to change the download directory for each test case at runtime.
Here is the 'TestBaseClass' code which downloading all the files in one particular folder from where I have to separate them as per category and place them into a particular folder.
There are 20 folders and each folder is having 2 subfolders 'ChapterLevel' & 'PracticeLevel' in which I do have to place it manually.
Is it possible to change the download directory by passing a variable during runtime?
My TestBaseClass code:
public static WebDriver driver;
public static void initialization() throws InvocationTargetException {
try {
// Setting new download directory path
Map<String, Object> prefs = new HashMap<String, Object>();
// Use File.separator as it will work on any OS
prefs.put("download.default_directory", "C:\\Users\\pd\\Desktop\\AHNPTTest");
// Adding cpabilities to ChromeOptions
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
// Launching browser with desired capabilities
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(options);
} catch (Exception e) {
// generic exception handling
e.printStackTrace();
}
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
}
Here is my testcase:
public class ANA_TC16_RiskAnalysisNewTest extends TestBaseClass {
ANA_RiskAnalysisNewPage New;
#BeforeMethod
public void setUp() {
try {
initialization();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
login();
New = new ANA_RiskAnalysisNewPage();
}
#Test
public void chapterrLevelTest() throws Exception {
New.hoverTest();
New.clickBottomOptions();
New.chapterOption();
New.TopX();
New.ATISlider();
New.conditionSelection();
New.takeScreenshot("Risk Analysis New Chapter Level Image");
New.downloadOptions();
New.isFileDownloaded();
}
#Test
public void practiceLevelTest() throws Exception {
New.hoverTest();
New.clickBottomOptions();
New.providerOption();
New.TopX();
New.ATISlider();
New.conditionSelection();
New.takeScreenshot("Risk Analysis New Practice Level Image");
New.downloadOptions();
New.isFileDownloaded();
}
}

Suppose you want to specify download folder for each test method.
Add parameter for downloadPath in initialization in TestBaseClass.
Add parameter for downloadPath in setup in ANA_TC16_RiskAnalysisNewTest, remove the #BerforMethod annotation and update each test method to call setup in begin with desired downloadPath.
public class TestBaseClass {
public static void initialization(String downloadPath) throws InvocationTargetException {
try {
// Setting new download directory path
Map<String, Object> prefs = new HashMap<String, Object>();
// Use File.separator as it will work on any OS
prefs.put("download.default_directory", downloadPath);
...
public class ANA_TC16_RiskAnalysisNewTest extends TestBaseClass {
ANA_RiskAnalysisNewPage New;
// #BeforeMethod
public void setUp(String downloadPath) {
try {
initialization(downloadPath);
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
login();
New = new ANA_RiskAnalysisNewPage();
}
#Test
public void chapterrLevelTest() throws Exception {
setUp("C:\\Users\\pd\\Desktop\\AHNPTTest\\ANA_TC16_RiskAnalysis\\ChapterLevel");
New.hoverTest();
...
}
#Test
public void practiceLevelTest() throws Exception {
setUp("C:\\Users\\pd\\Desktop\\AHNPTTest\\ANA_TC16_RiskAnalysis\\PracticeLevel");
New.hoverTest();
...
}
...

Related

Getting "java.lang.NullPointerException" error which trying to call the a function to take screenshot in a test method on Selenium [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
java.lang.NullPointerException is appearing
(1 answer)
Getting error exception in thread "main" java.lang.NullPointerException
(2 answers)
java.lang.NullPointerException using static WebDriver instance
(1 answer)
Closed 2 years ago.
I am new to the selenium automation world, Getting java.lang.NullPointerException error which trying to call the function to take a screenshot in a test method on Selenium. I am pretty sure I have missed initialize or return the driver somewhere. below is the code.
baseTest class where I am initializing the webdriver
public class baseTest {
public Properties objProp = new Properties();
FileInputStream readProp;
{
try {
readProp = new FileInputStream("C:\\Users\\kiran\\IdeaProjects\\SelProject2\\src\\test\\java\\appDetails.properties");
objProp.load(readProp);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected WebDriver driver;
#BeforeClass
public void beforeSuite()
{
System.setProperty(objProp.getProperty("browser"),objProp.getProperty("driverPath"));
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(objProp.getProperty("URL"));
}
#AfterSuite
public void afterSuite()
{
if(null != driver)
{
driver.close();
driver.quit();
}
}
public WebDriver getDriver()
{
return driver;
}
}
All my tests extends the baseTest function link shown below
public class loginFunction extends baseTest {
guruLoginPage objLogin;
guruHomePage objHome;
takeScreenshot objSS = new takeScreenshot(driver);
//readExcel readObj;
#Test
public void performLogin() throws IOException, InterruptedException {
objLogin = new guruLoginPage(driver);
String loginPageTitle = objLogin.getTitle();
Assert.assertTrue(loginPageTitle.contains("Demo Site"));
objSS.screenshot();
objLogin.loginAction(objProp.getProperty("appUsername"), objProp.getProperty("appPassword"));
Thread.sleep(2000);
objHome = new guruHomePage(driver);
Assert.assertTrue(objHome.validateLogin().contains("Manger Id : mngr242657"));
Thread.sleep(2000);
objSS.screenshot();
}
}
when i write this piece of code in the loginfunction, it works fine, but when i am trying to optimize and put it in a separate class and call the method I am getting java.lang.NullPointerException error
public class takeScreenshot{
WebDriver driver;
public takeScreenshot(WebDriver driver)
{
this.driver=driver;
PageFactory.initElements(driver,this);
}
public void screenshot() throws IOException {
String fileSuffix = DateTime.now().toString("yyyy-dd-MM-HH-mm-ss");
TakesScreenshot ss = ((TakesScreenshot)this.driver);
File srcFile = ss.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile,new File("C:\\Users\\kiran\\IdeaProjects\\SelProject2\\src\\test\\java\\Screenshots\\"+fileSuffix+".jpg"));
}
}

How does sparkjava(java webframe) auto refresh page

I`m new developper of spark, And now I was block by a issue.
I was implements freemarker as web template.
Not like other framework when you modify a .ftl file, You no need to restart the server.
But now in my local it must restart the server if I wanto see the change.
Below is code.
public class SparkServer {
public static void main(String[] args){
get("/hello",(request,response) ->{
Map root = new HashMap();
root.put("user", "xiekakaban");
Map product = new HashMap();
product.put("name","Pringles");
product.put("price",13.2);
root.put("product",product);
return new ModelAndView(root,"test.ftl");
},FreeMarkerEngine.getInstance());
}
}
public class FreeMarkerEngine extends TemplateEngine{
private static FreeMarkerEngine freeMarkerEngine;
private Configuration freeConfig;
private FreeMarkerEngine() throws IOException{
freeConfig = new Configuration();
freeConfig.setDirectoryForTemplateLoading(StringUtil.getResourceFile("templates"));
freeConfig.setTemplateUpdateDelay(1);
}
public static FreeMarkerEngine getInstance(){
if(freeMarkerEngine == null){
try {
freeMarkerEngine = new FreeMarkerEngine();
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
}
return freeMarkerEngine;
}
#Override
public String render(ModelAndView modelAndView) {
StringWriter stringWriter = new StringWriter();
try {
freeConfig.clearTemplateCache();
freeConfig.clearSharedVariables();
freeConfig.clearEncodingMap();
Template template = freeConfig.getTemplate(modelAndView.getViewName());
template.process(modelAndView.getModel(), stringWriter);
System.out.println(stringWriter.toString());
return stringWriter.toString();
} catch (IOException | TemplateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Can not find the template:"+modelAndView.getViewName();
}
}
I don`t sure whether it is cache by spark or freemarker.But I have clear freemarker cache.
Anyone can help me.....
ok, I have figure out it.
Reloading the static files in Spark/Jetty-server
first I think you should make sure which page the freemarker load.
if you not setup, it will load ftl under "target" fold.
I think I put out a stupid question....

Facing "nullpointer exception" in appium [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am learning appium and trying to call an object from one class to another and facing null pointer exception.
Below is my code :
public class TestCommons {
public AndroidDriver driver;
public void setUp() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "MotoE");
File file = new File("D:/APK1/com.vector.guru99.apk");
capabilities.setCapability("app", file);
try {
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public void tearDown() {
driver.closeApp();
}
}
I wanted to use above class i.e "TestCommons" in other class. I want to use driver object.
Second class is below :
public class Day03 extends TestCommons {
TestCommons commons = new TestCommons();
#BeforeClass
public void beforeClass() {
commons.setUp();
}
#Test(enabled = true)
public void f() {
if (driver.findElement(By.id("com.vector.guru99:id/action_quiz")).isDisplayed()) {
System.out.println("Quiz is displayed");
driver.findElement(By.id("com.vector.guru99:id/action_quiz")).click();
System.out.println("quiz is click");
}
}
#AfterClass(enabled = true)
public void afterClass() {
commons.tearDown();
}
}
Getting null pointer in second program #:
if(driver.findElement(By.id("com.vector.guru99:id/action_quiz")).isDisplayed();
Can anyone clarify me please.
You have one of two issues.
1) driver was not set properly in setUp(). If this is the case you probably got an exception. Check your logs to make sure that there isn't an exception there.
2) driver.findElement(By.id("com.vector.guru99:id/action_quiz")) is returning null. You can check this by setting a debug point and running evaluate expression on that call.
try this way:
public class TestCommons {
public static AndroidDriver driver;
#BeforeClass
public void setUp() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "MotoE");
File file = new File("D:/APK1/com.vector.guru99.apk");
capabilities.setCapability("app", file);
try {
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
#AfterClass
public void tearDown() {
driver.closeApp();
}
}
public class Day03 extends TestCommons {
#Test(enabled = true)
public void f() {
if (driver.findElement(By.id("com.vector.guru99:id/action_quiz")).isDisplayed()) {
System.out.println("Quiz is displayed");
driver.findElement(By.id("com.vector.guru99:id/action_quiz")).click();
System.out.println("quiz is click");
}
}
}

Webdriver is Not able to read element-Null Pointer exception..

I am Trying to Automate a Test sequence of Login - Booking - Cancellation.
Till Booking its Fine , But moment it Reaches Cancellation, It throws a java lang Null-Pointer Exp.
I rechecked and My locators(xpath) is correct(xpath-Checker) and I am Trying to getText() the Ticket Number and proceed to cancellation.
Problem is whenever WebDriver is reaching the Booking confirmation Page , which Loads after a While, it Fails and Returns Null Pointer Exp..
It may be a Loading Issue which is handled or I am messed up with my Java Concept...
Please Help !! anyone...
public class StackOverflow {
public static WebDriver driver;
public static Properties p;
public static FileInputStream f ;
#Test
public void loginTest() {
System.out.println("Enter Login");
Properties p=new Properties();
FileInputStream f = null;
try {
f = new FileInputStream("D:\\BOSSFramework\\Framework\\locators.properties");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
p.load(f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver = new FirefoxDriver();
driver.get("https://in3.seatseller.travel/");
driver.manage().window().maximize();
try{
driver.findElement(By.name(p.getProperty("login.username.textfield"))).sendKeys("UserName");
driver.findElement(By.name(p.getProperty("login.password.textfield"))).sendKeys("Password");
WebElement ele =driver.findElement(By.id(p.getProperty("login.signin.button")));
ele.click();
}
catch (Exception e) {
}
}
#Test (dependsOnMethods={"loginTest"})
public void booking() throws InterruptedException{
System.out.println("Enter Booking");
// Type Bangalore on Source Field..
Properties p=new Properties();
FileInputStream f = null;
try {
f = new FileInputStream("D:\\BOSSFramework\\Framework\\locators.properties");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
p.load(f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WebDriverWait wait2 = new WebDriverWait(driver, 30);
wait2.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(p.getProperty("oneapp.source.textfield"))));
driver.findElement(By.xpath(p.getProperty("oneapp.source.textfield"))).sendKeys("Bangalore");
driver.findElement(By.xpath(p.getProperty("oneapp.source.textfield"))).sendKeys(Keys.TAB);
Thread.sleep(900L);
// Type Mysore on Destination Field
WebDriverWait wait1 = new WebDriverWait(driver, 30);
wait1.until(ExpectedConditions.presenceOfElementLocated(By.xpath(p.getProperty("oneapp.destination.textfield"))));
driver.findElement(By.xpath(p.getProperty("oneapp.destination.textfield"))).sendKeys("Tirupathi");
driver.findElement(By.xpath(p.getProperty("oneapp.destination.textfield"))).sendKeys(Keys.TAB);
}
#Test (dependsOnMethods={"booking"})
public void cancellation() throws InterruptedException{
System.out.println("Enter Cancellation");
WebDriverWait wait4 = new WebDriverWait(driver, 60);
Thread.sleep(9000);
/*Facing Null Pointer Exp Here */
wait4.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(p.getProperty("oneapp.congratulations.text"))));
wait4.until(ExpectedConditions.presenceOfElementLocated(By.xpath(p.getProperty("oneapp.congratulations.text"))));
Thread.sleep(9000);
/*Facing Null Pointer Exp Here */
WebElement ticket =driver.findElement(By.xpath(p.getProperty("oneapp.ticket.text")));
/*Want to getText() Ticket Number to cancel*/
String ticket1 = ticket.getText();
System.out.println(ticket1);
driver.findElement(By.xpath(p.getProperty("oneapp.leftNavCancel.link "))).click();
WebDriverWait wait1 = new WebDriverWait(driver, 30);
wait1.until(ExpectedConditions.presenceOfElementLocated(By.xpath(p.getProperty("oneapp.phoneNumber.textfield"))));
driver.findElement(By.xpath(p.getProperty("oneapp.phoneNumber.textfield"))).sendKeys(TIN1);
driver.findElement(By.xpath(p.getProperty("oneapp.search.button"))).click();
driver.findElement(By.xpath(p.getProperty("oneapp.cancel.button"))).click();
}
}
Either add Properties p=new Properties(); in your cancellation() method or mention it outside of the test methods(In the begining, you've declared public static Properties p; rather define it there like public static Properties p = new Properties();)
In first two methods you create f = new FileInputStream but in the third method you read properties without it. Did you try to read properties again?

When does onException get triggered in WebDriverEventListener?

I am using a custom WebDriverEventListener in my Selenium tests for logging and screenshot purposes. It works fine:
When for example an element is not found in the browser window an exception is thrown by the webdriver and the onException() method is properly triggerd
#Override
public void onException(Throwable throwable, WebDriver driver) {
// do stuff
}
When I throw an Exception myself like this: throw new WebDriverException("my message"); the event is not triggered.
Can someone explain this behavior?
If you want to do some action when test failed or some exception, you can add to your test a rule(add in class where is #Before setUp()):
#Rule
public TestRule testWatcher = new TestWatcher() {
#Override
public void succeeded(Description test){
for (LogEntry log : driver.manage().logs().get(LogType.DRIVER).getAll()) {
System.out.println("Level:" + log.getLevel().getName());
System.out.println("Message:" + log.getMessage());
System.out.println("Time:" + log.getTimestamp());
System.out.println("-----------------------------------------------");
}
System.out.println();
#Override
public void failed(Throwable t, Description test) {
String testName = test.getClassName();
String subTestName = test.getMethodName();
String screenShotName = String.format("%s\\%s", testName, screenShotName);
if (driver instanceof TakesScreenshot) {
File tempFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
System.out.println(">>>>>>>>>>LOGS: " + yourDirForImages + "\\" + screenShotName + ".png");
FileUtils.copyFile(tempFile, new File(String.format("%s.png", screenShotName)));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Listener can be used for doing some code when its trigger some action, beforeCLick if there is header, logo, or footer.
public class ListenerMethodsImplementation extends AbstractWebDriverEventListener {
public void beforeClickOn(WebElement element, WebDriver myTestDriver) {
assertTrue("No Logo!", myTestDriver.findElements(By.id("logo")) == 1);
}
How to use it:
#Before
public void setUp() {
EventFiringWebDriver myTestDriver = new EventFiringWebDriver(driver);
ListenerMethodsImplementation myListener = new ListenerMethodsImplementation();
myTestDriver.register(myListener);
driver = myTestDriver;
}
How to get driver from listener: ((EventFiringWebDriver) driver).getWrappedDriver()
PS it's only a little portion from my code but i think this will help you.
Now I understand that because the listener is only registered to the WebDriver itelf it won't handle exceptions outsite the WebDriver.
In the abstract test case I did the following as part of a suggestion by Andrian Durlestean.
eventListener = new CustomWebDriverEventListener();
driver = new EventFiringWebDriver(driver).register(eventListener);
#Rule
public TestWatcher watchman = new TestWatcher() {
protected void failed(Throwable e, Description description) {
RuntimeException exception = (RuntimeException) e;
eventListener.onException(e, getDriver());
if (exception instanceof RuntimeException) {
throw exception;
}
};
};

Categories

Resources