在另一个类中调用一个类时,会自动打开多个浏览器窗口

时间:2015-11-09 06:37:54

标签: selenium selenium-webdriver testng

我创建了一个类,我在其中创建了测试自动化所需的所有方法。我面临的问题是,当我运行主类时,它运行正常。但是当我在其他类中调用该类时,它会打开2个浏览器窗口。测试在1和其他保持理想的情况下进行。当我对@After使用close()或quit()方法时,它会关闭理想窗口而不是我正在处理的窗口。

以下是我的参考代码段

主要课程

public class NewTest extends ProjectManagement{
ProjectManagement project1 = new ProjectManagement();


@Test
public void createPro() throws Throwable {
        project1.navigateCreate(UId,Password);
}

    @AfterTest
public void afterTest() {

    driver.quit();
}

}

测试类

public class MainActivity extends Activity implements OnQueryTextListener {

    ListView list ;
    ListAdapter listAdapter;
    ArrayList<String> listTest;
     String name;
     Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list = (ListView)findViewById(R.id.list);
        listTest = new ArrayList<String>( );
        list.setTextFilterEnabled(true);



        Scanner(Environment.getRootDirectory()+"/");

         if(listTest.size() != 0)
            {
                listAdapter=new ListAdapter(getApplicationContext(),listTest);
                list.setAdapter(listAdapter);

                list.setOnItemClickListener(new OnItemClickListener() 
                {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
                {

                     name = (String) ((TextView) view).getText();


                    view.setSelected(true);


                    uri = Uri.parse(listTest.get(position).toString());
                    File fl = new File(uri.toString());
                    deleteFile(uri.toString());
                    Log.e("msg", "deleted");
                    Toast.makeText(getApplicationContext(), name+".apk"+" is deleted from internal storage", Toast.LENGTH_SHORT).show();
                    /*if(fl.exists())
                    {
                        fl.delete();
                        Log.e("msg", "deleted");
                        Toast.makeText(getApplicationContext(), name+".apk"+" is deleted from internal storage", Toast.LENGTH_SHORT).show();
                    }*/


                    list.setSelection(position);




                }});





            }




    }


      private void Scanner(String path) {
            // TODO Auto-generated method stub
            {
                try 
                {
                        File fl = new File(path);
                        File[] listOfFiles = fl.listFiles();              

                        for (File listOfFile : listOfFiles)
                         {
                            String s = listOfFile.getName();

                            if(s.endsWith(".apk"))
                            {


                            // store file name in listSoundNames
                            int pos = s.lastIndexOf(".");
                            if (pos > 0)
                            {
                              name = s.substring(0, pos);

                            }
                            listTest.add(name);

                            }


                            /////////////////////////////////
                            File f = new File(path+s+"/");
                            if (f.exists() && f.isDirectory()) {
                            Scanner(path+s+"/");
                            }
                            ////////////////////////////////


                        }



                }
            catch (Exception e) { }
            }

            }
     /* private TextWatcher filterTextWatcher = new TextWatcher() {
            public void afterTextChanged(Editable s) {
        }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub

            }
      };
*/


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            // Inflate menu to add items to action bar if it is present.
            inflater.inflate(R.menu.main, menu);
            // Associate searchable configuration with the SearchView
            SearchManager searchManager =
                    (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView =
                    (SearchView) menu.findItem(R.id.menu_search).getActionView();
            searchView.setSubmitButtonEnabled(true);
            searchView.setSearchableInfo(
                    searchManager.getSearchableInfo(getComponentName()));


          //Sets the hint text to display in the query text field
          searchView.setQueryHint("...");

           searchView.setOnQueryTextListener(this);


            return true;
        }


        @Override
        public boolean onQueryTextChange(String newText) {
            // TODO Auto-generated method stub
            listAdapter.getFilter().filter(newText);

            return true;
        }


        @Override
        public boolean onQueryTextSubmit(String query) {
            // TODO Auto-generated method stub
            return true;
        }

     /*  
        @Override
        public boolean onQueryTextChange(String newText) {
            // TODO Auto-generated method stub

            if (TextUtils.isEmpty(newText)) {
                //clear text filter so that list displays all items
                list.clearTextFilter();
                } else {
                // apply filter so that list displays only
                //matching child items
                //list.setFilterText(newText.toString());
                //listTest.addTextChangedListener(filterTextWatcher);
                 //listAdapter.notifyDataSetChanged();
                //list.setSelectionAfterHeaderView();
                }
            listAdapter.getFilter().filter(newText);

            return true;
        }


        @Override
        public boolean onQueryTextSubmit(String query) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), query, Toast.LENGTH_SHORT).show();
            return true;
        }


      */
}

1 个答案:

答案 0 :(得分:1)

如果要扩展ProjectManagement,则不需要在子类上实例化它。通过这样做,您可以有效地创建类的两个实例,因此,WebDriver的两个实例(反过来生成两个浏览器窗口)。

因此,请删除以下内容:

ProjectManagement project1 = new ProjectManagement();

将createPro()方法更改为:

@Test
public void createPro() throws Throwable {
    navigateCreate(UId,Password);
}