无法使用量角器在新选项卡中打开firefox

时间:2017-12-27 22:22:57

标签: javascript firefox browser protractor

我使用量角器在chrome,firefox和IE中运行我的E2E案例。

以下代码在Chrome中运行良好 - 打开新标签并点击google.com

function openNewTab(linkname) {
return browser.driver.executeScript(function (arguments) {
    var linkname = arguments;
    (function (link) {
        document.body.appendChild(link);
        link.setAttribute('href', linkname);
        link.dispatchEvent((function (e) {
            e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, false, 0, null);
            return e;
        }(document.createEvent('MouseEvents'))))
    }(document.createElement('a')));
}, linkname);
}

对于firefox,新标签已打开,但google.com未被点击,标签会立即关闭并显示错误 - " AssertionError:WebDriverError:InternalError:过多的递归"

我也试过下面的代码。它在chrome中工作正常但在firefox中没有。

   function openNewTab(linkname) {
   browser.driver.executeScript(function(arguments) {
        var linkname = arguments;
    var d = document,
        a = d.createElement('a');
    a.target = '_blank';
    a.href = linkname;
    a.innerHTML = '.';
    d.body.appendChild(a);
    a.click();
}, linkname)
};

还有其他alt用于在firefox中使用量角器打开新标签吗?

我试着按照下面的回答,但是它会抛出。未定义错误。

public void editCustomerDetails()throws IOException, ClassNotFoundException
{
    File inFile=new File(fileName);
    FileInputStream inFileStream=new FileInputStream(inFile);
    ObjectInputStream inFileObjectStream = new ObjectInputStream(inFileStream);

    List CustomerList= new ArrayList();

    int noOfRecords= inFileObjectStream.readInt();//error java.io.EOFException, null in java.io.DataInputStream
    //get entire list(all records) from file
    try
    {
        CustomerList =(List)inFileObjectStream.readObject();
    }catch(Exception e)
    {
        System.out.println("Error reading file-no records");
        noOfRecords= -1;

    }
    UserDetails[] customer = new UserDetails[noOfRecords];
    String nameToSearchFor; 
    System.out.println("Enter the name you want to search for: ");
    nameToSearchFor= sc.next();
    int position =-1;
    boolean flag=false;
    for(int i=0; i<noOfRecords; i++)
    {
         CustomerList =(List)inFileObjectStream.readObject();

         if(CustomerList.contains(nameToSearchFor))
       {
           position = i;
           flag = true;

       }

    }

    if (flag == true)
    {   
        System.out.println("Account Number: " + customer[position].getAccountNum() + "\n" +
                              "Name: " + customer[position].getName() + "\n" + 
                              "Surname: "  + customer[position].getSurname() + "\n" +
                              "Email: " + customer[position].getEmail() + "\n" +
                              "Date of Birth: " + customer[position].getDateOfBirth()
                              + "\n" );

           String n,s,email,d;

           System.out.println("Edit name");
           n=sc.next();
           System.out.println("Edit surname");
           s=sc.next();
           System.out.println("Edit email");
           email=sc.next();
           System.out.println("Edit Date Of Birth");
           d=sc.next();

           //conversion of String to Date     
           try {
               d=sc.nextLine();
               DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
               Date date = formatter.parse(d);
               customer[position].setDateOfBirth(formatter.format(date));

           } catch (ParseException e) {
        //handle exception if date is not in "dd-MMM-yyyy" format
        System.out.println(e.getMessage());
       }

           customer[position].setName(n);
           customer[position].setSurname(s); 
           customer[position].setEmail(email); 
           CustomerList.add(customer);


           File outFile = new File(fileName);
           FileOutputStream outFileStream = new FileOutputStream(outFile);
           ObjectOutputStream outFileObjectStream = new ObjectOutputStream(outFileStream);


           outFileObjectStream.writeObject(CustomerList);
           outFileObjectStream.close();
    }
    else
    {
        flag=false;
        System.out.println("Customer was not found");
    }
  }

1 个答案:

答案 0 :(得分:0)

对于你的第二种方式,我认为firefox上的原因失败是firefox不支持dispatchEvent()或initMouseEvent()或createEvent()。

您可以尝试以下代码:

browser.driver.executeScript(function (url) {
        var d = document,
            a = d.createElement('a');
        a.target = '_blank';
        a.href = url;
        a.innerHTML = '.';
        d.body.appendChild(a);
        a.click();
    }, "https://google.com");