Java传递值

标签 java return-value

我有三个程序,

首先进行 Selenium 测试

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
import junit.framework.*;

public class MyTest extends SeleneseTestCase {

int flag_eco;

public void setUp() throws Exception {
    setUp("http://www.mysite.com/", "*iexplore");
}
public void testMyTest() throws Exception {
    selenium.open("/pages/static/homepage_logout.html");
    selenium.type("username", "myuser");
    selenium.type("password", "password");
    selenium.click("//input[@value='LOGIN']");
    selenium.waitForPageToLoad("30000");
    selenium.click("Confirm");
    selenium.waitForPageToLoad("30000");
    selenium.click("link=Applications");
    selenium.waitForPageToLoad("30000");
    selenium.click("link=Journey");
    selenium.waitForPageToLoad("30000");
    selenium.click("link=Launch Application (MUST BE LOGGED IN)");
    selenium.waitForPageToLoad("30000");
    if((selenium.isTextPresent("Please enter one of the following:")))
    {
        System.out.println("Journey Working Fine");
        flag_test= 0;
    }
    else
    {
        System.out.println("Journey Failed");
        flag_test = 1;
    }
    selenium.selectFrame("topmenu");
    selenium.click("link=Home");
}
public static Test suite() {
//method added
return new TestSuite(MyTest.class);
}
public void tearDown(){
//Added . Will be called when the test will complete
selenium.stop();
}

}

然后sendmail从selenium测试中获取值

      import java.util.*;


         public class SendMail
         {
         public void send()
         {


        MyTest Test = new MyTest();
        if (Test.flag_test==1)
          {
            System.out.println("Journey Failed");
        }
        else if(Test.flag_test==0)
          {
            System.out.println("Journey Working Fine");
          }

} }

主类调用两者

        import java.io.*;
     import javax.servlet.*;

 public class Test 
   {
public static void main(String args[]) 
{


    MyTest tes = new MyTest();
            junit.textui.TestRunner.run(tes.suite());

    SendMail se = new SendMail();
    se.send();

}
   }

如何将标志值从 MyTest 传递到 SendMail

最佳答案

  • 该标志应该是public static(我没有看到它在您提供的代码中定义) - 即

    public class MyTest {
         public static int flag;
         // the rest of the code
    }
    
  • send()中,您可以使用MyTest.flag_test来引用它

请注意,这不是传递数据的好方法,但就您的情况而言,没有更好的方法了。

我认为你正在做一些根本不应该做的事情。这是我的建议:

  • 将更改标志的代码移到测试之外
  • 将其包含在测试中的适当位置(就好像它在那里一样)
  • 也将其包含在 SendMail 中。

因此,您不需要调用测试来获取标志。

关于Java传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2237970/

相关文章:

Python - 函数有一个列表作为参数。如何在不更改第一个列表的情况下返回另一个列表?

c - 是否可以在 C 中返回不同的 "false"?

java - 使用 Java 从同一源创建 PDF、HTML 和可选 RTF 文档?

java - 为什么 CompareTo() 不起作用?

java - SpringAOP 与 AspectJ 的介绍

c - printf的返回类型是什么

java - android 中的 speex 支持

java - JOptionPane.showInternalMessageDialog 导致运行时错误

c - sprintf 的返回值影响数组

php 函数不返回 foreach 中 MySQL 查询的所有结果