简体   繁体   中英

How to close browser?

,I am learning programing and i have problem. I would like to open only one browser and close it, but... my program open two browsers and close one.

Cucumber hook:

public class Hooks {


@Before
public void Initalize() {
    Chrome ChromeRun = new Chrome();
    ChromeRun.StartChrome().get(Config.baseURL);
    System.out.println("BEFORE SCENARIO");

}

@After
public void TearDown() {
    Controller Close = new Controller();
    Close.CloseDriver();

}

First Class:

public class Chrome {


public WebDriver StartChrome() {
    System.setProperty("webdriver.chrome.driver", Config.ChromePath);
    ChromeDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    return driver;
}

Second class:

public class Controller {

Chrome Chrome = new Chrome();
public WebDriver driver = Chrome.StartChrome();

public void CloseDriver() {
    try {
        driver.quit();
    } catch (Exception e) {
        System.out.println(":: WebDriver stop error");
    }
    driver = null;
    System.out.println(":: WebDriver stopped");

}

Maybe i have wrong idea off doing this? I try to share the project on class parts.

Issue #1: you are ignore Java style conventions and declaring methods and variables with names that start with an uppercase letter. This causes problems:

  1. It cause other people who might otherwise help you to simply walk away. Java experts don't like reading other peoples' "stinky" code.

  2. It leads to all sorts of conceptual errors. For example,

     Close.CloseDriver();

    looks like a call of a static method. But it isn't. Why is this a problem? Well, if you write confusing code, you will confuse people. Other people for certain, and probably yourself as well.

You really need to read / understand / follow Java style conventions. Especially if you want other people to read your code.

Issue #2: it looks to me like you haven't thought through the purpose and lifecycles of your wrapper objects; ie Chrome , Controller and Hooks . Abstracting common code in your unit tests is fine if you do it properly. But if you don't have a clear mental model of how your abstractions need to work, you are better of writing simple duplicative code.


Now to the actual problem. Due to the 2 issues above, I'm not sure I am reading your code correctly, but it looks to me like the problem is here:

@After
public void TearDown() {
    Controller Close = new Controller();
    Close.CloseDriver();
}

OK, you are closing a ChromeDriver . But which one?

Answer: you are closing the driver that was created when you instantiated in the previous statement. That is a different ChromeDriver instance to the one that is set up by your Initialize method.

Solution: my advice would be to throw that code away and start again:

  • Decide what purpose each class serves, what each method is supposed to do, and how they are going to be used. I strongly suspect that you actually need just one class.

  • Stick rigidly to standard Java identifier conventions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM