![](/img/trans.png)
[英]How to pass the global variable from one Test class to another test class and select that value in a dropdown in selenium?
[英]how to pass a variable value from one class to another
我有兩個包,一個是com.firstBooks.series.db.parser,它具有一個Java文件XMLParser.java,另一個包是com.firstBooks.series79,它具有一個名為AppMain.NW的類,我想發送變量的值名為_xmlFileName frm AppMain類到XMLParser類中的xmlFile變量,我正在發布這兩個類的代碼,請幫幫我。
package com.firstBooks.series.db.parser;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import net.rim.device.api.xml.parsers.DocumentBuilder;
import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
import net.rim.device.api.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.firstBooks.series.db.Question;
public class XMLParser {
private Document document;
public static Vector questionList;
public static String xmlFile;
public XMLParser() {
questionList = new Vector();
}
public void parseXMl() throws SAXException, IOException,
ParserConfigurationException {
// Build a document based on the XML file.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream inputStream = getClass().getResourceAsStream(xmlFile);
document = builder.parse(inputStream);
}
public void parseDocument() {
Element element = document.getDocumentElement();
NodeList nl = element.getElementsByTagName("question");
if (nl != null && nl.getLength() > 0) {
for (int i = 0; i < nl.getLength(); i++) {
Element ele = (Element) nl.item(i);
Question question = getQuestions(ele);
questionList.addElement(question);
}
}
}
private Question getQuestions(Element element) {
String title = getTextValue(element, "title");
String choice1 = getTextValue(element, "choice1");
String choice2 = getTextValue(element, "choice2");
String choice3 = getTextValue(element, "choice3");
String choice4 = getTextValue(element, "choice4");
String answer = getTextValue(element, "answer");
String rationale = getTextValue(element, "rationale");
Question Questions = new Question(title, choice1,
choice2, choice3, choice4, answer, rationale);
return Questions;
}
private String getTextValue(Element ele, String tagName) {
String textVal = null;
NodeList nl = ele.getElementsByTagName(tagName);
if (nl != null && nl.getLength() > 0) {
Element el = (Element) nl.item(0);
textVal = el.getFirstChild().getNodeValue();
}
return textVal;
}
}
Nw AppMain類的代碼
//#preprocess
package com.firstBooks.series79;
import net.rim.device.api.ui.UiApplication;
import com.firstBooks.series.ui.screens.HomeScreen;
public class AppMain extends UiApplication {
public static String _xmlFileName;
public static boolean _Lite;
public static int _totalNumofQuestions;
public static void initialize(){
//#ifndef FULL
/*
//#endif
_xmlFileName = "/res/Series79_FULL.xml";
_totalNumofQuestions = 50;
_Lite = false;
//#ifndef FULL
*/
//#endif
//#ifndef LITE
/*
//#endif
_xmlFileName = "/res/Series79_LITE.xml";
_totalNumofQuestions = 10;
_Lite = true;
//#ifndef LITE
*/
//#endif
}
private AppMain() {
initialize();
pushScreen(new HomeScreen());
}
public static void main(String args[]) {
new AppMain().enterEventDispatcher();
}
}
您只需要輸入:
XMLParser.xmlFile = AppMain._xmlFileName;
例如在這里:
public static void main(String args[]) {
new AppMain().enterEventDispatcher();
XMLParser.xmlFile = AppMain._xmlFileName;
}
我不確定這是否是最佳設計,但請解決您的問題。
好的,您的設置方式如下:
XmlParser.xmlFile = _xmlFileName;
在您的AppMain中某個地方,比如說initialize方法,應該可以解決問題。 也許我不明白您的問題。 您似乎不希望xmlFile成為公共靜態變量,但這是您的選擇。
其他答案是正確的,但是使用這樣的靜態變量是一個比較嚴格的設計。 如果您以后要使用第二個XMLParser對象來解析另一個文件,它將導致問題。
而是,XMLParser構造函數可以將文件名作為參數:
public class XMLParser {
private Document document;
public static Vector questionList;
private static String xmlFile; // this can be private
public XMLParser(String xmlFile) {
this.xmlFile = xmlFile;
questionList = new Vector();
}
然后在應用程序的其他部分中,當您創建XMLParser時:
XMLParser xmlParser = new XMLParser(AppMain._xmlFileName);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.