0

I'm trying to start a new Private window using selenium Web Driver. I did a lot of Search to find a way out. Here are some interested links I have preferred.

Link 1

Link 2

According to these links I found out that following code would work for me.

// Open a new Private Window
FirefoxProfile firefoxProfile = new FirefoxProfile();    
firefoxProfile.setPreference("browser.private.browsing.autostart",true);

But, this code in not working. I don't even get an error message. But, this code does not open a new private window.

If someone know the reason please help. Thanks in advance.

Updated Code

public class bCanGoAfterThreeMin {

    public void bCanGo() {

        WebDriver driver = new FirefoxDriver();

        //Puts an Implicit wait, Will wait for 25 seconds before throwing exception
        driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);


        //Launch site
        driver.navigate().to("my url");
        System.out.println("Map Page Launched");


        //Maximize the browser
        driver.manage().window().maximize();
        System.out.println("Browser Maximized");

        //Login
        driver.findElement(By.xpath(".//*[@id='login_btn']")).click();
        driver.findElement(By.xpath(".//*[@id='loginform']/div[5]/input")).sendKeys("manuli@et.lk");
        driver.findElement(By.xpath(".//*[@id='loginform']/div[6]/input")).sendKeys("1qaz2wsx");
        driver.findElement(By.xpath(".//*[@id='btn-login']")).click();


        // Click on a Hotel
        driver.findElement(By.xpath(".//*[@id='image_row']/div/div[1]/div/span/a/img")).click();

        if ((driver.getCurrentUrl())!= ("correct url")){
                System.out.println("Successfully redirect to the Aliya hotel Page");
            }
        else{
                System.out.println("Failed to redirect to the Aliya hotel Page");
            }


        // Click Book Now Button
        driver.findElement(By.cssSelector(".btn.btn-primary.btn-lg.book-btn")).click();             
        System.out.println("Successfully redirect to the cart page(With Aliya)");


        if ((driver.getCurrentUrl())!= ("correct url")){

        driver.navigate().to("correct url");

        // Click Proceed to checkout
        driver.findElement(By.cssSelector(".btn.btn-success.pro-btn")).click(); 

        System.out.println("Clicked Proceed to checkout");

        }

        // Open a new Private Window
        FirefoxProfile firefoxProfile = new FirefoxProfile();    
        firefoxProfile.setPreference("browser.private.browsing.autostart",true);

        driver = new FirefoxDriver();
        //driver.get("url");

        System.out.println("New Private Window");


    }
}
budi
  • 6,351
  • 10
  • 55
  • 80
  • You did not initialize web driver. Write `driver = new FirefoxDriver(); ` `driver.get("https://www.google.com");` – Helping Hands Dec 31 '15 at 04:32
  • Where should I put this? After FirefoxProfile firefoxProfile = new FirefoxProfile(); firefoxProfile.setPreference("browser.private.browsing.autostart",true); or before? –  Dec 31 '15 at 04:34
  • Also declare first driver like `WebDriver driver;` – Helping Hands Dec 31 '15 at 04:38
  • This opened a new Window. But, after that it is continusly running the whole class again and again. Why is that? –  Dec 31 '15 at 04:40
  • Can you please your whole class code by update question? so I can check in detail. – Helping Hands Dec 31 '15 at 04:41
  • Yeah Sure. I'll update the question –  Dec 31 '15 at 04:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99368/discussion-between-helping-hands-and-manu). – Helping Hands Dec 31 '15 at 04:46

2 Answers2

0

Hey if you wan to use any Firefox Profile you need to launch your driver with that firefox profile, whenever instantiating driver pass the profile in the constructor.

FirefoxProfile firefoxProfile = new FirefoxProfile();    
firefoxProfile.setPreference("browser.private.browsing.autostart",true);
WebDriver driver = new FirefoxDriver(firefoxProfile);
Paras
  • 3,197
  • 2
  • 20
  • 30
0

It seems you have not initialize driver , please change code as per below so it will work as per your need :

public void bCanGo() { 

WebDriver driver; 

driver = new FirefoxDriver(); 
System.out.println("New Private Window"); 

//Puts an Implicit wait, Will wait for 25 seconds before throwing exception 
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS); 


//Launch site 
driver.navigate().to("my url"); 
System.out.println("Map Page Launched"); 


//Maximize the browser 
driver.manage().window().maximize(); 
System.out.println("Browser Maximized"); 

//Login 
driver.findElement(By.xpath(".//*[@id='login_btn']")).click(); 
driver.findElement(By.xpath(".//*[@id='loginform']/div[5]/input")).sendKeys("manuli@et.lk"); 
driver.findElement(By.xpath(".//*[@id='loginform']/div[6]/input")).sendKeys("1qaz2wsx"); 
driver.findElement(By.xpath(".//*[@id='btn-login']")).click(); 


// Click on a Hotel 
driver.findElement(By.xpath(".//*[@id='image_row']/div/div[1]/div/span/a/img")).click(); 

if ((driver.getCurrentUrl())!= ("correct url")){ 
System.out.println("Successfully redirect to the Aliya hotel Page"); 
} 
else{ 
System.out.println("Failed to redirect to the Aliya hotel Page"); 
} 


// Click Book Now Button 
driver.findElement(By.cssSelector(".btn.btn-primary.btn-lg.book-btn")).click(); 
System.out.println("Successfully redirect to the cart page(With Aliya)"); 


if ((driver.getCurrentUrl())!= ("correct url")){ 

driver.navigate().to("correct url"); 

// Click Proceed to checkout 
driver.findElement(By.cssSelector(".btn.btn-success.pro-btn")).click(); 

System.out.println("Clicked Proceed to checkout"); 

} 


// Open a new Private Window 
FirefoxProfile firefoxProfile = new FirefoxProfile(); 
firefoxProfile.setPreference("browser.private.browsing.autostart",true); 

driver = new FirefoxDriver(); 
driver.get("url"); 

System.out.println("New Private Window"); 

}

Note : I have declared driver and assign with firefox.

Helping Hands
  • 5,292
  • 9
  • 60
  • 127