Java方法写入显示奇怪行为的文件

时间:2017-08-29 19:55:46

标签: java selenium-webdriver

那里的代码专家的问题。我有一个奇怪的Java问题,也许有人有一些想法。我有一个名为logger()的方法,它只是写入控制台和文件。

 public static void logger(String UrlMessage) throws IOException {
    //
    // Write any errors out to a log file.
    //
    Date date = new Date();
    Format formatter = new SimpleDateFormat("YYYY-MM-dd");
    String savestr = "C:\\DiversityTest\\Logs\\DiversityTestLog_" + formatter.format(date) + ".txt";
    File f = new File(savestr);

    PrintWriter out = null;
    if ( f.exists() && !f.isDirectory() ) {
        out = new PrintWriter(new FileOutputStream(new File(savestr), true));
        out.println(UrlMessage + "\n\r");
        out.close();
    } else {
        out = new PrintWriter(savestr);
        out.println(UrlMessage + "\n\r");
        out.close();
    }
        System.out.println(UrlMessage);
}

非常标准的东西。

以下是SetProperty()的代码。

 public static void SetPropertyValue(String xpathValue, String myPropertyName) throws InterruptedException {

    // Instead of reading each property value from the file we're going to pass them in from the call in Main()

    // Start propotron
    //DiversityTest.propotronStart();

    // variable initialization
    BufferedReader br;
    String x;

    try {
        br = new BufferedReader(new FileReader("C:\\DiversityTest\\propotron.ini"));
        try {                   
            // We're going to loop through the input file one line at a time until we hit null <EOF>. 
            while ((x = br.readLine()) != null) {

                // parse the property name and value
                String tokens[] = null; 
                tokens = x.split(" ");
                String propertyName = tokens[0];
                String propertyValue = tokens[1];
                WebElement myElement;

                if (propertyName.contentEquals("appver")) {
                    WebElement droplist = (new WebDriverWait(driver, 30))  
                            .until(ExpectedConditions.elementToBeClickable(By.id("applicationInput")));
                    droplist.click();

                    droplist = (new WebDriverWait(driver, 30))  
                            .until(ExpectedConditions.elementToBeClickable(By.id("applicationName" + propertyValue)));
                    droplist.click();


                    myElement = (new WebDriverWait(driver, 30))
                            .until(ExpectedConditions.elementToBeClickable(By.id("propertyFilter")));
                    //System.out.println("The text box contains " + myElement.getAttribute("value"));
                    if (!myElement.getAttribute("value").contains("bfm")) {
                        myElement.sendKeys("bfm");
                    }
                    Thread.sleep(2000);
                    propotronCheckBoxes();

                }

                if (propertyName.contentEquals(myPropertyName)) {
                    int attempts = 0;
                    while (attempts < 2) {
                        try {
                            myElement = (new WebDriverWait(driver, 30))
                                    .until(ExpectedConditions.elementToBeClickable(By.xpath(xpathValue)));
                            myElement.click();
                            break;
                        } catch (StaleElementReferenceException e) {
                            attempts++;
                        }
                    }

                    // now the contents of the edit box have a different ID so get it and set it
                    attempts = 0;
                    while (attempts < 2) {
                        try {
                            myElement = (new WebDriverWait(driver, 30))
                                    .until(ExpectedConditions.elementToBeClickable(By.id(myPropertyName)));
                            myElement.clear();
                            myElement.sendKeys(propertyValue);
                            //System.out.println("We set " + myPropertyName);
                            break;
                        } catch (StaleElementReferenceException e) {
                            attempts++;
                        }
                    }
                    DiversityTest.propotronSave();
                    DiversityTest.prcLogger("Property " + myPropertyName + " set to " + propertyValue);                                 
                }
            }
        } catch (IOException e) {
            System.out.println(e);
            e.printStackTrace();
        }
    } catch (IOException e) {
        System.out.println(e);
        e.printStackTrace();
    }
    Thread.sleep(1000);         
    //driver.quit();
}

其他几种方法调用logger()。其中一个特别称为SetPropertyValue()。每当SetPropertyValue()调用logger()时,它当然会将文本写入PC上的文件。这就是诀窍 - 在同事PC上却没有。

现在我们有另一个名为GetGDSInfo()的方法,它也调用了logger。这种方法记录器在两台PC上运行良好。

两种方法都声明为public static void。这两种方法都使用Selenium webdriver。

我完全难过了。

我注意到的一件事是运行1.7.0_79并且她正在运行1.8.0_0121。 我不认为这是因为如果两种方法都调用logger()会显示相同的行为。

我完全难过了。任何人的想法?

0 个答案:

没有答案