0

I tried to write some regex to determine client device details such as OS name, browser daetils still facing issue for IE browser detection.

Code for Browser detection:

 public static final BrowserBean getBrowserInfo(String userAgent){
    BrowserBean browserBean = new BrowserBean();
    Pattern pattern = Pattern.compile("(?i)(opera|chrome|safari|firefox|MSIE|trident)(\\s|\\/)(\\d+.\\d+)");
    Matcher matcher = pattern.matcher(userAgent);
    while (matcher.find()) {
        int groupCount = matcher.groupCount();
        if(groupCount >= 3){
        browserBean.setBrowserName(matcher.group(1));
        browserBean.setBrowserVersion(matcher.group(3));
        }
      }
    return browserBean; 
    }

Test case:

 @Test
    public void detectSafaritest() {
    String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.71 (KHTML, like Gecko) Version/6.1 Safari/537.71";
    BrowserBean bean =  UserAgentDetermination.getBrowserInfo(userAgent);
    Assert.assertEquals("Safari", bean.getBrowserName());
    Assert.assertEquals("537.71", bean.getBrowserVersion());
    }

    @Test
    public void detectIEtest() {
    String userAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; MASP)";
    BrowserBean bean =  UserAgentDetermination.getBrowserInfo(userAgent);
    Assert.assertEquals("InterNet Explorer", bean.getBrowserName());
    Assert.assertEquals("10.0", bean.getBrowserVersion());
    }

    @Test
    public void detectFireFoxtest() {
    String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0";
    BrowserBean bean =  UserAgentDetermination.getBrowserInfo(userAgent);
    Assert.assertEquals("Firefox", bean.getBrowserName());
    Assert.assertEquals("25.0", bean.getBrowserVersion());
    }

//    @Test
//    public void detectOperatest() {
//  String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.71 (KHTML, like Gecko) Version/6.1 Safari/537.71";
//  BrowserBean bean =  UserAgentDetermination.getBrowserInfo(userAgent);
//  Assert.assertEquals("Safari", bean.getBrowserName());
//  Assert.assertEquals("537.71", bean.getBrowserVersion());
//    }

    @Test
    public void detectChrometest() {
    String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36";
    BrowserBean bean =  UserAgentDetermination.getBrowserInfo(userAgent);
    Assert.assertEquals("Chrome", bean.getBrowserName());
    Assert.assertEquals("30.0", bean.getBrowserVersion());
    }

I am having issue for IE and trident browser, as I can see the user agent is printing as : Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; MASP) Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko

Please help me to identify IE all version successfully.

will appreciate if you are helping me with device info as well.

Jayaram
  • 1,715
  • 18
  • 30

2 Answers2

0

Your setters BrowserBean.setBrowserName and BrowserBean.setBrowserVersion will be called twice because by using find in a while loop you get multiple matches:

  • MSIE 9.0
  • Trident/5.0

Assuming your setters are just plain setters, The Trident match will override the MSIE match.

Nappy
  • 3,016
  • 27
  • 39
-1

Your Regex is fine, the problem is in your assert statement. For IE, the string contains "MSIE", and that is what you are looking for (and returning). But your assert statement is looking for the string "InterNet Explorer"

One minor point though: the last part of your regex should really be (\\d+\\.\\d+) instead of (\\d+.\\d+). That way it will match a literal . rather than any single character.

EvilBob22
  • 732
  • 5
  • 12
  • Nha!!…in my bean i have like this if("MSIE".equalsIgnoreCase(this.browserName)){ this.browserName = "InterNet Explorer"; } return browserName; – Jayaram Nov 08 '13 at 19:33
  • version part is not having issue …its giving correct details if it has Safari/9537.53 or Trident/6.0 etc….. – Jayaram Nov 08 '13 at 19:38
  • Then you need this http://stackoverflow.com/questions/1266943/how-to-get-multiple-regex-matches-in-java – EvilBob22 Nov 08 '13 at 20:39