2

I need to start my JAVA Selenium code in current Window of Firefox. But when I start my code WebDriver driver = new FirefoxDriver(); will open new window! Don't need open new window! In Selenium IDE code works in current window of browser.

package tm.csgo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Item {

public String siteUrl;
public String url;
public String name;
public int bought;
public int needCount;
public int wantPrice;
public double price;

public void buyProcess() {

    System.out.println("Начало покупки, запуск браузера...");
    System.out.println("Запланировано приобрести " + needCount + " " + name + " предметов.");

    WebDriver driver = new FirefoxDriver();


    while(bought <= needCount){

        System.out.println("Приобретаем " + bought + " предмет");
        driver.get(siteUrl + url);
        price = Double.parseDouble(driver.findElement(By.className("ip-bestprice")).getText());

        System.out.println("Стоимость предмета " + name + ": " + price + " рублей");

        if (price <= wantPrice) {

            System.out.println("Покупаем " + name + "...");
            driver.findElement(By.cssSelector("a.buy-pic-button")).click();
            driver.findElement(By.id("info_dialog_url")).click();

        } else {
            continue;
            }

        System.out.println("Приобретён " + bought + " предмет за " + price + " рублей из " + needCount + " запланированных предметов");
        bought++;
    }

}

public void purchaseReady() {
    //дописать общую сумму затрат
    System.out.println("Приобретено предметов на сумму ...");
    System.out.println("Покупка успешно завершена!");
}

}

GoldenScrew
  • 181
  • 2
  • 3
  • 14
  • What version of Selenium are you using? This is possible with both WebDriver and Selenium2. Not sure about other versions though. – Brian Mar 16 '16 at 20:45
  • Last versions of Selenium and Java. – GoldenScrew Mar 16 '16 at 21:15
  • 2.53 is it possible? – GoldenScrew Mar 16 '16 at 21:16
  • You are likely calling `driver.quit()` then. You should post your code so we can look it over. – Brian Mar 16 '16 at 21:16
  • my code is simple. I start with it: `WebDriver driver = new FirefoxDriver();`And next I need use site, but site should have cookies for use it. But I can't log in in every code, because site: http://steamcommunity.com/ Where for log in I should use mobile steam guard... – GoldenScrew Mar 16 '16 at 21:22
  • Like I mentioned above, are you calling `driver.quit();`? Are you using any annotations like `@BeforeMethod` or `@AfterMethod`? Calling `.quit()` will kill the instance of the browser. – Brian Mar 16 '16 at 21:42
  • If I use `driver.quit();` I have an error: `Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException: The FirefoxDriver cannot be used after quit() was called.`. I added my code in main question. Please tell me know, where I should use `driver.quit();` and how? – GoldenScrew Mar 17 '16 at 08:15

1 Answers1

0

In my case I am using Selenium with junit for running separate tests. Here is basically the architecture:

  1. I have a singleton Context class that creates instance of Selenium WebDriver, depending on the browser. As part of the implementation I am checking if webDriver = null;. For the browser if (browser == Browser.IE) and etc. Here is the code that returns WebDriver instance:

    public WebDriver getWebDriver() {
        if (webDriver != null) {
            return webDriver;
        }
    }
    
  2. I have a base test class that instantiates the Context one. In @Before I have a setup method for browser starting:

    @Before
    public final void setUp() {
        startBrowser();
    }
    

    Here is the startBrowser method:

    private void startBrowser() {
        context.getWebDriver().get("about:blank"); 
    }
    
  3. As part of the basic test class I have also @AfterClass annotation that quits the browser:

    @AfterClass
    public static void tearDownClass() {
        Context.getSingleton().quit();
    }
    
  4. The last steps is to include all @Test annotations between @Before and @AfterClass ones. All of them will be executed without browser restarting. If you decide to develop this framework further I would suggest you to create another class that extends the test one and which contains only the tests (all @Test methods). Also in my case I am using the PageObject pattern. All this will help you for a better test framework structure and will optimize your code in case of changes.

Hope this help you!

Michaël Azevedo
  • 3,874
  • 7
  • 31
  • 45