Unable to bind to locking port 70 54 within 45000 ms

后端 未结 4 2005
傲寒
傲寒 2021-01-25 08:23

I\'m getting this error when I\'m trying to run my selenium test using MVN test command line. Curiously, I tried it 3 days ago and it ran successfully:

---------         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-25 08:50

    Late reply, but try this updated code. Selenium Webdriver binds the instance to a specific TCP port. In the case your test fails and the driver is not properly closed, the port will be held on to for x amount of time. Using driver.Quit() will generally free up the TCP port.

    To test to see which port might currently still be held on to, you can use the netstat -a command (windows) to find the list of active connections.

    A way to get around / side step this issue, is allow selenium to bind to another port that is generated by your code. Most ports above 7000 to 7100 are fine to use. Selenium's built in way to handle the ports is to initially try to bind to 7055, if that fails, bind to 7054, if that fails bind to 7056. In MOST test cases this is fine, but I have found that multiple tests still encounter failure. So instead of using the defaults, specify your own to the profile.

    import java.util.List;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxProfile;
    import org.testng.annotations.Test;
    
    public class GoogleNavigationTest {
    @Test
    public void testApp(){
    
        // Specify a new or randomly generated port for the driver to use
        int genPort = 7052;
        // The Firefox driver supports javascript 
        FirefoxProfile firefoxProfile = new FirefoxProfile();
        //In the profile, assign it a different port to use instead of 7054,7055,7056
        //In my tests, I have a method that will generate a port to use that is open
        firefoxProfile.Port = genPort; 
        WebDriver driver = new FirefoxDriver(firefoxProfile);
    

    Edit: Diving deeper into this issue has revealed that two things contribute to this error. The first being the TCP / UDP Port selection on your local machine, the second being the amount of time that selenium must copy the base firefox profile from the drive to it's temp folder. The slower the transfer, the more likely it is to have the port binding issue.

    To break down the issue, make your profile as small as possible. This can involve deleting some base firefox files that are generated upon startup. My firefox profile is > 5 MB in size. Before I did this research my profile size was over 60 MB. At the start of each test, it would try to transfer 60MB to a temp location and bind to a locking port.

    My new code hasnt failed me

        var smallerProfile = @"C:\Firefox Profiles\SmallProfile";
        var genPort = new Random(); 
    
         FirefoxProfile profile = new FirefoxProfile(smallerProfile);
         profile.Clean();
         profile.Port = genPort.Next(7000, 7500);
    

    I copy over the smaller profile at the start of each main run.

提交回复
热议问题