【问题标题】:maven profile can't get the propertyMaven配置文件无法获取该属性
【发布时间】:2020-12-07 14:41:56
【问题描述】:

请告诉我可能是什么问题?

测试因错误而崩溃: 失败:java.lang.NullPointerException:条目中的空值:url=null

pom.xml:

 <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.21.0</version>
                    <configuration>
                        <systemPropertyVariables>
                            <db.url>https://dev.site.com/</db.url>
                        </systemPropertyVariables>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

BaseTest.java:

@BeforeClass (alwaysRun = true)
public void setUp() {

    Logger.getLogger("com.dataart.demo.java.logging.SomeClass").log(Level.INFO,System.getProperty("db.url"));

     if (System.getProperty("os.name").toLowerCase().contains("win")) {
         driverPath = "src/test/resources/chromedriver86.exe";
     }
     else {
         driverPath = "src/test/resources/chromedriverlinux86";
     }
    System.setProperty("webdriver.chrome.driver", driverPath);
    LoggingPreferences logs = new LoggingPreferences();
    logs.enable(LogType.BROWSER, Level.SEVERE);
    ChromeOptions options = new ChromeOptions();
    HashMap<String, Object> chromePref = new HashMap<>();
    chromePref.put("profile.default_content_settings.popups", 0);
    chromePref.put("download.default_directory", System.getProperty("user.dir"));
    options.setExperimentalOption("prefs", chromePref);
    options.addArguments("headless");
    options.addArguments("window-size=1800,1000");
    options.setCapability(CapabilityType.LOGGING_PREFS, logs);
    options.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
    driver = new ChromeDriver(options);
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10L, SECONDS);
    driver.get(System.getProperty("db.url")); // line 71
} 

失败:java.lang.NullPointerException:条目中的空值:url=null

http://joxi.ru/D2PqDR9uJ8PBlA

【问题讨论】:

  • 输出中提到的第 71 行是哪几行?你如何开始测试?顺便提一句。请将 Maven 输出添加为文本而不是屏幕截图。
  • 通过故障安全插件中的配置传递系统属性应该工作:使用 maven-failsafe-plugin:2.22.2测试>

标签: java selenium maven selenium-webdriver testng


【解决方案1】:

如果您开始单独的测试,您的配置应该可以工作。 (在 IDE 中)

但测试会在你做mvn installmvn test,...

失败:java.lang.NullPointerException:条目中的空值: url=null

那你还应该配置surefire-plugin

<profile>
    <id>dev</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <systemPropertyVariables>
                        <db.url>https://dev.site.com/</db.url>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <systemPropertyVariables>
                        <db.url>https://dev.site.com/</db.url>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

【讨论】:

    【解决方案2】:

    从 maven 的角度来看,我认为您应该查看下面的这个 sn-p(这是从以前的答案中得出的):

    1. surefire/failsafe 配置进入pluginManagement。如果您需要覆盖一个模块的 systemProperty,您可以这样做。如果需要,您还可以合并/替换父配置(请参阅此 Q/A:Append or merge default maven plugin configuration)。
    2. 两者的配置都将注入一个系统属性db.url,其值为${test.db.url}
    3. 该属性的值在配置文件中配置。

    这旨在避免重复surefire/failsafe 配置,尤其是当db.url 不是您需要配置的单一属性时。

    您还可以使用 maven 资源插件从过滤文件中读取此内容(同时避免使用全局共享上下文,例如:System.getProperty)。而且我个人认为这是最好的(我更喜欢配置文件而不是系统属性,前者更容易共享)。

    <project>
      <build>
        <pluginManagement>
          <plugins>
            <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.22.2</version> <configuration>
              <systemPropertyVariables>
                <db.url>${test.db.url}</db.url>
              </systemPropertyVariables>
            </configuration> </plugin> 
            <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration>
              <systemPropertyVariables>
                <db.url>${test.db.url}</db.url>
              </systemPropertyVariables>
            </configuration> </plugin>
          </plugins>
        </pluginManagement>
      </build>
      <profile>
        <id>dev</id>
        <activation> <activeByDefault>true</activeByDefault> </activation>
        <properties>
          <test.db.url>https://dev.site.com/</test.db.url>
        </properties>
      </profile>
    </project>
    

    【讨论】:

      猜你喜欢
      • 2017-01-24
      • 2018-11-03
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      • 2018-04-13
      • 2019-12-13
      • 1970-01-01
      相关资源
      最近更新 更多