Wednesday, September 13, 2017

Proxy setting to browser level using selenium with java



Let's say if your application work with proxy values on your browser, so you need to set the proxy server and port number before invoking the application  so here is the code.


import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

public class ProxyTest {

    @Test
    public void testMethod(){
    WebDriver driver;

       String usedProxy = "IPAddress:port";

        Proxy proxy = new org.openqa.selenium.Proxy();
        proxy.setHttpProxy(usedProxy).setFtpProxy(usedProxy).setSslProxy(usedProxy);
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability(CapabilityType.PROXY, proxy);

        driver = new FirefoxDriver(cap);
       
        driver.get("yoursite.com/");
}
}

No comments:

Post a Comment

TestNG - Can i use the 2 different data providers to same @test methods in TestNG?

public Object [][] dp1 () { return new Object [][] { new Object [] { "a" , "b" }, new Obje...