<commit_before>package pt.fccn.mobile.arquivo.pages;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class AdvancedSearchPage {
	private final WebDriver driver;
	private final String topicsToSearch = "expo 98";
	private final String negTerm = "concerto";
	private final String expressionTerm = "\"pavilhao da utopia\"";
	private final String[ ] terms = new String[ ]{ "pavilhao da utopia", "pavilhao",  "da",  "utopia", "expo" , "98" };
	private final String siteSearch = "www.arquivo.pt";
	private final int timeout = 50;
	
    public AdvancedSearchPage( WebDriver driver ) {
        this.driver = driver;
        try {
          Thread.sleep( 5000 );                 //wait for page to load
        } catch( InterruptedException ex ) {
          Thread.currentThread( ).interrupt( );
        }  
        
        
        // Check that we're on the right page.
        String pageTitle= driver.getTitle( );
        System.out.println( "pageTile = " + pageTitle );
        
    }
    
    public boolean checkSiteOperator( String  language ) {
    	System.out.println(  "[checkSiteOperator]" );
    	String inputSearch 		= "//*[@id=\"adv_and\"]";
    	String divExpandable 	= "//*[@id=\"domains\"]/legend/i";
    	String inputSiteSearch 	= "//*[@id=\"site\"]"; 
    	String buttonSearch 	= "//*[@id=\"btnSubmitBottom\"]";
    	
		try{
			if( language.equals( "EN" ) ) {
				switchLanguage( );
			}
			
			if( searchSiteSearch( inputSearch , inputSiteSearch , divExpandable , buttonSearch ) )
				return true;
			else 
				return false;
			
    	} catch( NoSuchElementException e ) {
            System.out.println( "Error in checkOPSite" );
            e.printStackTrace( );
            return false;
    	}
    }
    
	private boolean searchSiteSearch( String inputSearch, String inputSiteSearch, String divExpandable, String buttonSearch ) {
		System.out.println( "[searchSiteSearch]" );
         
    	System.out.println( "Search for " + topicsToSearch );
		WebElement elementSearch = ( new WebDriverWait( driver, timeout ) ) /* Wait Up to 50 seconds should throw RunTimeExcpetion*/
                .until(
                		ExpectedConditions.presenceOfElementLocated( 
                				By.xpath( inputSearch ) ) );
		elementSearch.clear( );
		elementSearch.sendKeys( topicsToSearch );
        
	    WebElement divElement = ( new WebDriverWait( driver, timeout ) ) /* Wait Up to 50 seconds should throw RunTimeExcpetion*/
	            .until(
	            		ExpectedConditions.presenceOfElementLocated(
	            				By.xpath( divExpandable ) ) );
	    divElement.click( );
	        
	    sleepThread( );
	    
		System.out.println( "Search for site \"" + siteSearch+ "\"" );
        WebElement elementExpression = ( new WebDriverWait( driver, timeout ) ) /* Wait Up to 50 seconds should throw RunTimeExcpetion*/
                .until(
                		ExpectedConditions.presenceOfElementLocated( 
                				By.xpath( inputSiteSearch ) ) );
        elementExpression.clear( );
        elementExpression.sendKeys( siteSearch );
        
        WebElement btnSubmitElement = ( new WebDriverWait( driver, timeout ) ) /* Wait Up to 50 seconds should throw RunTimeExcpetion*/
            .until(
            		ExpectedConditions.presenceOfElementLocated(
            				By.xpath( buttonSearch ) ) );
        btnSubmitElement.click( );
        
        sleepThread( );
       
        if( !checkSiteResults( siteSearch ) )
        	return false;

        return true;
	}
		
	private boolean checkSiteResults( String siteSearch ) {
		System.out.println( "[AdvancedSearch][checkSiteResults]" );
		String getURLResults = "//*[@id=\"resultados-lista\"]/ul/li/div[1]";
		String domainSearch = expandURL( siteSearch.toLowerCase( ).trim( ) );
		boolean checkURL = false;
	    try{
    		List< WebElement > results = ( new WebDriverWait( driver, timeout ) )
	                .until( ExpectedConditions
	                        .visibilityOfAllElementsLocatedBy(
	                        		      By.xpath( getURLResults )
	                        )
	        );
    		
    		System.out.println( "results size = " + results.size( ) );
    		for( WebElement elem : results ) {
    			String url = elem.getText( ).toLowerCase( ).trim( );
    			String treatedURL = expandURL( url );
				System.out.println( "[AdvancedSearch][checkSiteResults] domainSearch["+domainSearch+"] equals treatedURL["+treatedURL+"]" );
				if( !domainSearch.equals( treatedURL ) ) 
					checkURL = false;
    		}
	    	
    		if( !checkURL )
    			return false;
    		else
    			return true;
    		
	    } catch( NoSuchElementException e ){
            System.out.println( "Error in checkOPSite" );
            e.printStackTrace( );
            return false;
    	}
	}
    
    public boolean checkAdvancedSearch( String language ) {
    	System.out.println(  "[checkAdvancedSearch]" );
    	String inputSearch = "//*[@id=\"adv_and\"]";
    	String inputExpression = "//*[@id=\"adv_phr\"]";<commit_msg>[Mobile-tests][AdvancedSearch] fixed bug<commit_after>package pt.fccn.mobile.arquivo.pages;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class AdvancedSearchPage {
	private final WebDriver driver;
	private final String topicsToSearch = "expo 98";
	private final String negTerm = "concerto";
	private final String expressionTerm = "\"pavilhao da utopia\"";
	private final String[ ] terms = new String[ ]{ "<NAME>", "pavilhao",  "da",  "utopia", "expo" , "98" };
	private final String siteSearch = "www.arquivo.pt";
	private final int timeout = 50;
	
    public AdvancedSearchPage( WebDriver driver ) {
        this.driver = driver;
        try {
          Thread.sleep( 5000 );                 //wait for page to load
        } catch( InterruptedException ex ) {
          Thread.currentThread( ).interrupt( );
        }  
        
        
        // Check that we're on the right page.
        String pageTitle= driver.getTitle( );
        System.out.println( "pageTile = " + pageTitle );
        
    }
    
    public boolean checkSiteOperator( String  language ) {
    	System.out.println(  "[checkSiteOperator]" );
    	String inputSearch 		= "//*[@id=\"adv_and\"]";
    	String divExpandable 	= "//*[@id=\"domains\"]/legend/i";
    	String inputSiteSearch 	= "//*[@id=\"site\"]"; 
    	String buttonSearch 	= "//*[@id=\"btnSubmitBottom\"]";
    	
		try{
			if( language.equals( "EN" ) ) {
				switchLanguage( );
			}
			
			if( searchSiteSearch( inputSearch , inputSiteSearch , divExpandable , buttonSearch ) )
				return true;
			else 
				return false;
			
    	} catch( NoSuchElementException e ) {
            System.out.println( "Error in checkOPSite" );
            e.printStackTrace( );
            return false;
    	}
    }
    
	private boolean searchSiteSearch( String inputSearch, String inputSiteSearch, String divExpandable, String buttonSearch ) {
		System.out.println( "[searchSiteSearch]" );
        
    	System.out.println( "Search for " + topicsToSearch );
		WebElement elementSearch = ( new WebDriverWait( driver, timeout ) ) /* Wait Up to 50 seconds should throw RunTimeExcpetion*/
                .until(
                		ExpectedConditions.presenceOfElementLocated( 
                				By.xpath( inputSearch ) ) );
		elementSearch.clear( );
		elementSearch.sendKeys( topicsToSearch );
        
	    WebElement divElement = ( new WebDriverWait( driver, timeout ) ) /* Wait Up to 50 seconds should throw RunTimeExcpetion*/
	            .until(
	            		ExpectedConditions.presenceOfElementLocated(
	            				By.xpath( divExpandable ) ) );
	    divElement.click( );
	        
	    sleepThread( );
	    
		System.out.println( "Search for site \"" + siteSearch+ "\"" );
        WebElement elementExpression = ( new WebDriverWait( driver, timeout ) ) /* Wait Up to 50 seconds should throw RunTimeExcpetion*/
                .until(
                		ExpectedConditions.presenceOfElementLocated( 
                				By.xpath( inputSiteSearch ) ) );
        elementExpression.clear( );
        elementExpression.sendKeys( siteSearch );
        
        WebElement btnSubmitElement = ( new WebDriverWait( driver, timeout ) ) /* Wait Up to 50 seconds should throw RunTimeExcpetion*/
            .until(
            		ExpectedConditions.presenceOfElementLocated(
            				By.xpath( buttonSearch ) ) );
        btnSubmitElement.click( );
        
        sleepThread( );
       
        if( !checkSiteResults( siteSearch ) )
        	return false;

        return true;
	}
		
	private boolean checkSiteResults( String siteSearch ) {
		System.out.println( "[AdvancedSearch][checkSiteResults]" );
		String getURLResults = "//*[@id=\"resultados-lista\"]/ul/li/div[1]";
		String domainSearch = expandURL( siteSearch.toLowerCase( ).trim( ) );

	    try{
    		List< WebElement > results = ( new WebDriverWait( driver, timeout ) )
	                .until( ExpectedConditions
	                        .visibilityOfAllElementsLocatedBy(
	                        		      By.xpath( getURLResults )
	                        )
	        );
    		
    		System.out.println( "results size = " + results.size( ) );
    		for( WebElement elem : results ) {
    			String url = elem.getText( ).toLowerCase( ).trim( );
    			String treatedURL = expandURL( url );
				System.out.println( "[AdvancedSearch][checkSiteResults] domainSearch["+domainSearch+"] equals treatedURL["+treatedURL+"]" );
				if( !domainSearch.equals( treatedURL ) ) 
					return false;
    		}
	    	
    		return true;
    		
	    } catch( NoSuchElementException e ){
            System.out.println( "Error in checkOPSite" );
            e.printStackTrace( );
            return false;
    	}
	}
    
    public boolean checkAdvancedSearch( String language ) {
    	System.out.println(  "[checkAdvancedSearch]" );
    	String inputSearch = "//*[@id=\"adv_and\"]";
    	String inputExpression = "//*[@id=\"adv_phr\"]";
{'fraction_non_alphanumeric': 0.11469806304595519, 'fraction_numerical': 0.0036080516521078618, 'mean_word_length': 2.368404221298369, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7484747', 'n_tokens_mistral': 3279, 'n_tokens_neox': 3018, 'n_words': 681}