Searching for cosmological gravitational-wave backgrounds with third-generation detectors in the presence of an astrophysical foreground