简体   繁体   English

如何关闭浏览器?

[英]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.问题 #1:您忽略了 Java 风格约定并声明了名称以大写字母开头的方法和变量。 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. Java 专家不喜欢阅读其他人的“臭”代码。

  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.您确实需要阅读/理解/遵循 Java 样式约定。 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;问题#2:在我看来,您似乎没有考虑过包装器对象的用途和生命周期; ie Chrome , Controller and Hooks .ChromeControllerHooks 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:由于上述 2 个问题,我不确定我是否正确阅读了您的代码,但在我看来问题出在这里:

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

OK, you are closing a ChromeDriver .好的,您正在关闭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.这是一个与您的Initialize方法设置的ChromeDriver实例不同的实例。

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.严格遵守标准的 Java 标识符约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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